
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 02/16/2010 20:18:23
-- Generated from EDMX file: D:\Projetos\HPCERP\HPCERP.Model\Model1.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [hpcerp_test];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Account_Account]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Account] DROP CONSTRAINT [FK_Account_Account];
GO
IF OBJECT_ID(N'[dbo].[FK_Account_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Account] DROP CONSTRAINT [FK_Account_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Bank_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Bank] DROP CONSTRAINT [FK_Bank_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_BankAccount_Bank]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BankAccount] DROP CONSTRAINT [FK_BankAccount_Bank];
GO
IF OBJECT_ID(N'[dbo].[FK_BankCheckLayout_Bank]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BankCheckLayout] DROP CONSTRAINT [FK_BankCheckLayout_Bank];
GO
IF OBJECT_ID(N'[dbo].[FK_BankTransaction_Account]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BankTransaction] DROP CONSTRAINT [FK_BankTransaction_Account];
GO
IF OBJECT_ID(N'[dbo].[FK_BankTransaction_BankAccount]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BankTransaction] DROP CONSTRAINT [FK_BankTransaction_BankAccount];
GO
IF OBJECT_ID(N'[dbo].[FK_BankTransaction_Client]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BankTransaction] DROP CONSTRAINT [FK_BankTransaction_Client];
GO
IF OBJECT_ID(N'[dbo].[FK_BankTransaction_CostCenter]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BankTransaction] DROP CONSTRAINT [FK_BankTransaction_CostCenter];
GO
IF OBJECT_ID(N'[dbo].[FK_BankTransaction_Supplier]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[BankTransaction] DROP CONSTRAINT [FK_BankTransaction_Supplier];
GO
IF OBJECT_ID(N'[dbo].[FK_City_Country]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[City] DROP CONSTRAINT [FK_City_Country];
GO
IF OBJECT_ID(N'[dbo].[FK_City_State]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[City] DROP CONSTRAINT [FK_City_State];
GO
IF OBJECT_ID(N'[dbo].[FK_Client_ClientType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Client] DROP CONSTRAINT [FK_Client_ClientType];
GO
IF OBJECT_ID(N'[dbo].[FK_Client_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Client] DROP CONSTRAINT [FK_Client_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_ClientAdress_Address_Type]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ClientAddress] DROP CONSTRAINT [FK_ClientAdress_Address_Type];
GO
IF OBJECT_ID(N'[dbo].[FK_ClientAdress_City]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ClientAddress] DROP CONSTRAINT [FK_ClientAdress_City];
GO
IF OBJECT_ID(N'[dbo].[FK_ClientAdress_Client]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ClientAddress] DROP CONSTRAINT [FK_ClientAdress_Client];
GO
IF OBJECT_ID(N'[dbo].[FK_ClientContact_Client]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ClientContact] DROP CONSTRAINT [FK_ClientContact_Client];
GO
IF OBJECT_ID(N'[dbo].[FK_ClientType_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ClientType] DROP CONSTRAINT [FK_ClientType_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_CostCenter_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CostCenter] DROP CONSTRAINT [FK_CostCenter_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Debt_Account]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Debt] DROP CONSTRAINT [FK_Debt_Account];
GO
IF OBJECT_ID(N'[dbo].[FK_Debt_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Debt] DROP CONSTRAINT [FK_Debt_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Debt_CostCenter]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Debt] DROP CONSTRAINT [FK_Debt_CostCenter];
GO
IF OBJECT_ID(N'[dbo].[FK_Debt_Supplier]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Debt] DROP CONSTRAINT [FK_Debt_Supplier];
GO
IF OBJECT_ID(N'[dbo].[FK_DebtParcels_BankTransaction]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DebtParcels] DROP CONSTRAINT [FK_DebtParcels_BankTransaction];
GO
IF OBJECT_ID(N'[dbo].[FK_DebtParcels_Debt]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DebtParcels] DROP CONSTRAINT [FK_DebtParcels_Debt];
GO
IF OBJECT_ID(N'[dbo].[FK_DebtPayment_BankTransaction]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DebtPayment] DROP CONSTRAINT [FK_DebtPayment_BankTransaction];
GO
IF OBJECT_ID(N'[dbo].[FK_DebtPayment_DebtParcels]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DebtPayment] DROP CONSTRAINT [FK_DebtPayment_DebtParcels];
GO
IF OBJECT_ID(N'[dbo].[FK_DebtRecurring_Account]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DebtRecurring] DROP CONSTRAINT [FK_DebtRecurring_Account];
GO
IF OBJECT_ID(N'[dbo].[FK_DebtRecurring_CostCenter]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DebtRecurring] DROP CONSTRAINT [FK_DebtRecurring_CostCenter];
GO
IF OBJECT_ID(N'[dbo].[FK_DebtRecurring_RecurringType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DebtRecurring] DROP CONSTRAINT [FK_DebtRecurring_RecurringType];
GO
IF OBJECT_ID(N'[dbo].[FK_DebtRecurring_Supplier]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DebtRecurring] DROP CONSTRAINT [FK_DebtRecurring_Supplier];
GO
IF OBJECT_ID(N'[dbo].[FK_Invoice_Client]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Invoice] DROP CONSTRAINT [FK_Invoice_Client];
GO
IF OBJECT_ID(N'[dbo].[FK_Invoice_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Invoice] DROP CONSTRAINT [FK_Invoice_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Invoice_Orders]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Invoice] DROP CONSTRAINT [FK_Invoice_Orders];
GO
IF OBJECT_ID(N'[dbo].[FK_Invoice_Seller]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Invoice] DROP CONSTRAINT [FK_Invoice_Seller];
GO
IF OBJECT_ID(N'[dbo].[FK_InvoiceItens_Invoice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[InvoiceItens] DROP CONSTRAINT [FK_InvoiceItens_Invoice];
GO
IF OBJECT_ID(N'[dbo].[FK_InvoiceItens_Product]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[InvoiceItens] DROP CONSTRAINT [FK_InvoiceItens_Product];
GO
IF OBJECT_ID(N'[dbo].[FK_InvoiceItens_UnitMeasurement]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[InvoiceItens] DROP CONSTRAINT [FK_InvoiceItens_UnitMeasurement];
GO
IF OBJECT_ID(N'[dbo].[FK_InvoicePayments_Invoice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[InvoicePayments] DROP CONSTRAINT [FK_InvoicePayments_Invoice];
GO
IF OBJECT_ID(N'[dbo].[FK_Module_ModuleGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Module] DROP CONSTRAINT [FK_Module_ModuleGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderItens_Orders]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderItens] DROP CONSTRAINT [FK_OrderItens_Orders];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderItens_Product]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderItens] DROP CONSTRAINT [FK_OrderItens_Product];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderItens_UnitMeasurement]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderItens] DROP CONSTRAINT [FK_OrderItens_UnitMeasurement];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderPayments_Orders]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderPayments] DROP CONSTRAINT [FK_OrderPayments_Orders];
GO
IF OBJECT_ID(N'[dbo].[FK_Orders_Client]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Orders] DROP CONSTRAINT [FK_Orders_Client];
GO
IF OBJECT_ID(N'[dbo].[FK_Orders_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Orders] DROP CONSTRAINT [FK_Orders_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Orders_Proposal]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Orders] DROP CONSTRAINT [FK_Orders_Proposal];
GO
IF OBJECT_ID(N'[dbo].[FK_Orders_Seller]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Orders] DROP CONSTRAINT [FK_Orders_Seller];
GO
IF OBJECT_ID(N'[dbo].[FK_Product_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Product] DROP CONSTRAINT [FK_Product_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Product_Product_Type]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Product] DROP CONSTRAINT [FK_Product_Product_Type];
GO
IF OBJECT_ID(N'[dbo].[FK_Product_UnitMeasurement]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Product] DROP CONSTRAINT [FK_Product_UnitMeasurement];
GO
IF OBJECT_ID(N'[dbo].[FK_ProductType_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProductType] DROP CONSTRAINT [FK_ProductType_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Project_Client]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Project] DROP CONSTRAINT [FK_Project_Client];
GO
IF OBJECT_ID(N'[dbo].[FK_Proposal_Client]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Proposal] DROP CONSTRAINT [FK_Proposal_Client];
GO
IF OBJECT_ID(N'[dbo].[FK_Proposal_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Proposal] DROP CONSTRAINT [FK_Proposal_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Proposal_Seller]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Proposal] DROP CONSTRAINT [FK_Proposal_Seller];
GO
IF OBJECT_ID(N'[dbo].[FK_ProposalItens_Product]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProposalItens] DROP CONSTRAINT [FK_ProposalItens_Product];
GO
IF OBJECT_ID(N'[dbo].[FK_ProposalItens_Proposal]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProposalItens] DROP CONSTRAINT [FK_ProposalItens_Proposal];
GO
IF OBJECT_ID(N'[dbo].[FK_ProposalItens_UnitMeasurement]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProposalItens] DROP CONSTRAINT [FK_ProposalItens_UnitMeasurement];
GO
IF OBJECT_ID(N'[dbo].[FK_ProposalPayments_Proposal]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ProposalPayments] DROP CONSTRAINT [FK_ProposalPayments_Proposal];
GO
IF OBJECT_ID(N'[dbo].[FK_Receivable_Account]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Receivable] DROP CONSTRAINT [FK_Receivable_Account];
GO
IF OBJECT_ID(N'[dbo].[FK_Receivable_BankTransaction]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Receivable] DROP CONSTRAINT [FK_Receivable_BankTransaction];
GO
IF OBJECT_ID(N'[dbo].[FK_Receivable_Client]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Receivable] DROP CONSTRAINT [FK_Receivable_Client];
GO
IF OBJECT_ID(N'[dbo].[FK_Receivable_CostCenter]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Receivable] DROP CONSTRAINT [FK_Receivable_CostCenter];
GO
IF OBJECT_ID(N'[dbo].[FK_ReceivableParcels_BankTransaction]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ReceivableParcels] DROP CONSTRAINT [FK_ReceivableParcels_BankTransaction];
GO
IF OBJECT_ID(N'[dbo].[FK_ReceivableParcels_Receivable]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ReceivableParcels] DROP CONSTRAINT [FK_ReceivableParcels_Receivable];
GO
IF OBJECT_ID(N'[dbo].[FK_ReceivablePayment_BankTransaction]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ReceivablePayment] DROP CONSTRAINT [FK_ReceivablePayment_BankTransaction];
GO
IF OBJECT_ID(N'[dbo].[FK_ReceivablePayment_ReceivableParcels]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ReceivablePayment] DROP CONSTRAINT [FK_ReceivablePayment_ReceivableParcels];
GO
IF OBJECT_ID(N'[dbo].[FK_ReceivableRecurring_Account]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ReceivableRecurring] DROP CONSTRAINT [FK_ReceivableRecurring_Account];
GO
IF OBJECT_ID(N'[dbo].[FK_ReceivableRecurring_Client]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ReceivableRecurring] DROP CONSTRAINT [FK_ReceivableRecurring_Client];
GO
IF OBJECT_ID(N'[dbo].[FK_ReceivableRecurring_CostCenter]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ReceivableRecurring] DROP CONSTRAINT [FK_ReceivableRecurring_CostCenter];
GO
IF OBJECT_ID(N'[dbo].[FK_ReceivableRecurring_RecurringType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ReceivableRecurring] DROP CONSTRAINT [FK_ReceivableRecurring_RecurringType];
GO
IF OBJECT_ID(N'[dbo].[FK_Rights_Module]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Rights] DROP CONSTRAINT [FK_Rights_Module];
GO
IF OBJECT_ID(N'[dbo].[FK_Rights_SecurityGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Rights] DROP CONSTRAINT [FK_Rights_SecurityGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_SecurityGroup_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SecurityGroup] DROP CONSTRAINT [FK_SecurityGroup_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_SecurityGroup_Users_SecurityGroup]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SecurityGroup_Users] DROP CONSTRAINT [FK_SecurityGroup_Users_SecurityGroup];
GO
IF OBJECT_ID(N'[dbo].[FK_SecurityGroup_Users_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SecurityGroup_Users] DROP CONSTRAINT [FK_SecurityGroup_Users_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_Seller_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Seller] DROP CONSTRAINT [FK_Seller_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_ServiceRequest_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ServiceRequest] DROP CONSTRAINT [FK_ServiceRequest_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_ShortCut_PageLink]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ShortCut] DROP CONSTRAINT [FK_ShortCut_PageLink];
GO
IF OBJECT_ID(N'[dbo].[FK_ShortCut_PageLinkSource]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ShortCut] DROP CONSTRAINT [FK_ShortCut_PageLinkSource];
GO
IF OBJECT_ID(N'[dbo].[FK_State_Country]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[State] DROP CONSTRAINT [FK_State_Country];
GO
IF OBJECT_ID(N'[dbo].[FK_Stock_Product]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Stock] DROP CONSTRAINT [FK_Stock_Product];
GO
IF OBJECT_ID(N'[dbo].[FK_StockTransaction_Product]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockTransaction] DROP CONSTRAINT [FK_StockTransaction_Product];
GO
IF OBJECT_ID(N'[dbo].[FK_StockTransaction_TransactionType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StockTransaction] DROP CONSTRAINT [FK_StockTransaction_TransactionType];
GO
IF OBJECT_ID(N'[dbo].[FK_Supplier_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Supplier] DROP CONSTRAINT [FK_Supplier_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_Supplier_SupplierType]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Supplier] DROP CONSTRAINT [FK_Supplier_SupplierType];
GO
IF OBJECT_ID(N'[dbo].[FK_SupplierAdress_Address_Type]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SupplierAddress] DROP CONSTRAINT [FK_SupplierAdress_Address_Type];
GO
IF OBJECT_ID(N'[dbo].[FK_SupplierAdress_City]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SupplierAddress] DROP CONSTRAINT [FK_SupplierAdress_City];
GO
IF OBJECT_ID(N'[dbo].[FK_SupplierAdress_Supplier]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SupplierAddress] DROP CONSTRAINT [FK_SupplierAdress_Supplier];
GO
IF OBJECT_ID(N'[dbo].[FK_SupplierContact_Supplier]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SupplierContact] DROP CONSTRAINT [FK_SupplierContact_Supplier];
GO
IF OBJECT_ID(N'[dbo].[FK_SupplierType_Company]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SupplierType] DROP CONSTRAINT [FK_SupplierType_Company];
GO
IF OBJECT_ID(N'[dbo].[FK_TroubleTicket_Project]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TroubleTicket] DROP CONSTRAINT [FK_TroubleTicket_Project];
GO
IF OBJECT_ID(N'[dbo].[FK_Users_CostCenter]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Users] DROP CONSTRAINT [FK_Users_CostCenter];
GO
IF OBJECT_ID(N'[dbo].[FK_ServiceRequest_Historic_ServiceRequest]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ServiceRequest_Historic] DROP CONSTRAINT [FK_ServiceRequest_Historic_ServiceRequest];
GO
IF OBJECT_ID(N'[dbo].[FK_ServiceRequest_Historic_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ServiceRequest_Historic] DROP CONSTRAINT [FK_ServiceRequest_Historic_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_ServiceRequest_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[ServiceRequest] DROP CONSTRAINT [FK_ServiceRequest_User];
GO
IF OBJECT_ID(N'[dbo].[FK_TimeSheet_ServiceRequest]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TimeSheet] DROP CONSTRAINT [FK_TimeSheet_ServiceRequest];
GO
IF OBJECT_ID(N'[dbo].[FK_TimeSheet_TroubleTicket]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TimeSheet] DROP CONSTRAINT [FK_TimeSheet_TroubleTicket];
GO
IF OBJECT_ID(N'[dbo].[FK_TimeSheet_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TimeSheet] DROP CONSTRAINT [FK_TimeSheet_User];
GO
IF OBJECT_ID(N'[dbo].[FK_TroubleTicket_Historic_TroubleTicket]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TroubleTicket_Historic] DROP CONSTRAINT [FK_TroubleTicket_Historic_TroubleTicket];
GO
IF OBJECT_ID(N'[dbo].[FK_TroubleTicket_Historic_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TroubleTicket_Historic] DROP CONSTRAINT [FK_TroubleTicket_Historic_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_TroubleTicket_User]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[TroubleTicket] DROP CONSTRAINT [FK_TroubleTicket_User];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Account]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Account];
GO
IF OBJECT_ID(N'[dbo].[Address_Type]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Address_Type];
GO
IF OBJECT_ID(N'[dbo].[Bank]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Bank];
GO
IF OBJECT_ID(N'[dbo].[BankAccount]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BankAccount];
GO
IF OBJECT_ID(N'[dbo].[BankAccountBalance]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BankAccountBalance];
GO
IF OBJECT_ID(N'[dbo].[BankCheckLayout]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BankCheckLayout];
GO
IF OBJECT_ID(N'[dbo].[BankTransaction]', 'U') IS NOT NULL
    DROP TABLE [dbo].[BankTransaction];
GO
IF OBJECT_ID(N'[dbo].[City]', 'U') IS NOT NULL
    DROP TABLE [dbo].[City];
GO
IF OBJECT_ID(N'[dbo].[Client]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Client];
GO
IF OBJECT_ID(N'[dbo].[ClientAddress]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ClientAddress];
GO
IF OBJECT_ID(N'[dbo].[ClientContact]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ClientContact];
GO
IF OBJECT_ID(N'[dbo].[ClientType]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ClientType];
GO
IF OBJECT_ID(N'[dbo].[Company]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Company];
GO
IF OBJECT_ID(N'[dbo].[CostCenter]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CostCenter];
GO
IF OBJECT_ID(N'[dbo].[CountParam]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CountParam];
GO
IF OBJECT_ID(N'[dbo].[Country]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Country];
GO
IF OBJECT_ID(N'[dbo].[Debt]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Debt];
GO
IF OBJECT_ID(N'[dbo].[DebtParcels]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DebtParcels];
GO
IF OBJECT_ID(N'[dbo].[DebtPayment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DebtPayment];
GO
IF OBJECT_ID(N'[dbo].[DebtRecurring]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DebtRecurring];
GO
IF OBJECT_ID(N'[dbo].[Invoice]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Invoice];
GO
IF OBJECT_ID(N'[dbo].[InvoiceItens]', 'U') IS NOT NULL
    DROP TABLE [dbo].[InvoiceItens];
GO
IF OBJECT_ID(N'[dbo].[InvoicePayments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[InvoicePayments];
GO
IF OBJECT_ID(N'[dbo].[LogMessage]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LogMessage];
GO
IF OBJECT_ID(N'[dbo].[Module]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Module];
GO
IF OBJECT_ID(N'[dbo].[ModuleGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ModuleGroup];
GO
IF OBJECT_ID(N'[dbo].[OrderItens]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderItens];
GO
IF OBJECT_ID(N'[dbo].[OrderPayments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderPayments];
GO
IF OBJECT_ID(N'[dbo].[Orders]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Orders];
GO
IF OBJECT_ID(N'[dbo].[PageLink]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PageLink];
GO
IF OBJECT_ID(N'[dbo].[Phrase]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Phrase];
GO
IF OBJECT_ID(N'[dbo].[Product]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Product];
GO
IF OBJECT_ID(N'[dbo].[ProductType]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProductType];
GO
IF OBJECT_ID(N'[dbo].[Project]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Project];
GO
IF OBJECT_ID(N'[dbo].[Proposal]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Proposal];
GO
IF OBJECT_ID(N'[dbo].[ProposalItens]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProposalItens];
GO
IF OBJECT_ID(N'[dbo].[ProposalPayments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ProposalPayments];
GO
IF OBJECT_ID(N'[dbo].[Receivable]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Receivable];
GO
IF OBJECT_ID(N'[dbo].[ReceivableParcels]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ReceivableParcels];
GO
IF OBJECT_ID(N'[dbo].[ReceivablePayment]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ReceivablePayment];
GO
IF OBJECT_ID(N'[dbo].[ReceivableRecurring]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ReceivableRecurring];
GO
IF OBJECT_ID(N'[dbo].[RecurringType]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RecurringType];
GO
IF OBJECT_ID(N'[dbo].[Rights]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Rights];
GO
IF OBJECT_ID(N'[dbo].[SecurityGroup]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SecurityGroup];
GO
IF OBJECT_ID(N'[dbo].[SecurityGroup_Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SecurityGroup_Users];
GO
IF OBJECT_ID(N'[dbo].[Seller]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Seller];
GO
IF OBJECT_ID(N'[dbo].[ServiceRequest]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ServiceRequest];
GO
IF OBJECT_ID(N'[dbo].[ServiceRequest_Historic]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ServiceRequest_Historic];
GO
IF OBJECT_ID(N'[dbo].[ShortCut]', 'U') IS NOT NULL
    DROP TABLE [dbo].[ShortCut];
GO
IF OBJECT_ID(N'[dbo].[State]', 'U') IS NOT NULL
    DROP TABLE [dbo].[State];
GO
IF OBJECT_ID(N'[dbo].[Stock]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Stock];
GO
IF OBJECT_ID(N'[dbo].[StockTransaction]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StockTransaction];
GO
IF OBJECT_ID(N'[dbo].[Supplier]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Supplier];
GO
IF OBJECT_ID(N'[dbo].[SupplierAddress]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SupplierAddress];
GO
IF OBJECT_ID(N'[dbo].[SupplierContact]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SupplierContact];
GO
IF OBJECT_ID(N'[dbo].[SupplierType]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SupplierType];
GO
IF OBJECT_ID(N'[dbo].[Teste]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Teste];
GO
IF OBJECT_ID(N'[dbo].[TimeSheet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TimeSheet];
GO
IF OBJECT_ID(N'[dbo].[TransactionType]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TransactionType];
GO
IF OBJECT_ID(N'[dbo].[TroubleTicket]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TroubleTicket];
GO
IF OBJECT_ID(N'[dbo].[TroubleTicket_Historic]', 'U') IS NOT NULL
    DROP TABLE [dbo].[TroubleTicket_Historic];
GO
IF OBJECT_ID(N'[dbo].[UnitMeasurement]', 'U') IS NOT NULL
    DROP TABLE [dbo].[UnitMeasurement];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'Account'
CREATE TABLE [dbo].[Account] (
    [ID_Account] int IDENTITY(1,1) NOT NULL,
    [AccountNumber] varchar(50)  NOT NULL,
    [AccountType] int  NOT NULL,
    [Name] varchar(150)  NOT NULL,
    [Id_ParentAccount] int  NULL,
    [Id_Company] int  NULL
);
GO

-- Creating table 'Address_Type'
CREATE TABLE [dbo].[Address_Type] (
    [Id_Address_Type] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(100)  NULL
);
GO

-- Creating table 'Bank'
CREATE TABLE [dbo].[Bank] (
    [ID_Bank] int IDENTITY(1,1) NOT NULL,
    [Id_Company] int  NOT NULL,
    [Name] varchar(50)  NOT NULL
);
GO

-- Creating table 'BankAccount'
CREATE TABLE [dbo].[BankAccount] (
    [ID_BankAccount] int IDENTITY(1,1) NOT NULL,
    [Id_Bank] int  NOT NULL,
    [Agency] varchar(10)  NULL,
    [AccountNumber] varchar(15)  NULL,
    [AccountDay] int  NULL,
    [AccountType] int  NULL,
    [AccountName] varchar(30)  NULL,
    [InitialBalance] decimal(19,4)  NULL
);
GO

-- Creating table 'BankAccountBalance'
CREATE TABLE [dbo].[BankAccountBalance] (
    [Id_BankAccount] int  NOT NULL,
    [BalanceDate] datetime  NOT NULL,
    [Balance] decimal(19,4)  NULL
);
GO

-- Creating table 'BankCheckLayout'
CREATE TABLE [dbo].[BankCheckLayout] (
    [Id_Bank] int  NOT NULL,
    [ValueLine] int  NULL,
    [ValueColumn] int  NULL,
    [ValueTextLine1] int  NULL,
    [ValueColumnLine1] int  NULL,
    [ValueTextLine2] int  NULL,
    [ValueColumnLine2] int  NULL,
    [NameLine] int  NULL,
    [NameColumn] int  NULL,
    [CityLine] int  NULL,
    [CityColumn] int  NULL,
    [DayLine] int  NULL,
    [DayColumn] int  NULL,
    [MonthLine] int  NULL,
    [MonthColumn] int  NULL,
    [YearLine] int  NULL,
    [YearColumn] int  NULL
);
GO

-- Creating table 'BankTransaction'
CREATE TABLE [dbo].[BankTransaction] (
    [ID_BankTransaction] int IDENTITY(1,1) NOT NULL,
    [Id_BankAccount] int  NOT NULL,
    [BankTransactionType] int  NOT NULL,
    [TransactionDate] datetime  NOT NULL,
    [Document] varchar(15)  NULL,
    [Description] varchar(100)  NULL,
    [Id_CostCenter] int  NULL,
    [Id_Account] int  NULL,
    [Id_Supplier] int  NULL,
    [Id_Client] int  NULL,
    [TransactionValue] decimal(19,4)  NULL,
    [Id_BankAccount_Transfer] int  NULL
);
GO

-- Creating table 'City'
CREATE TABLE [dbo].[City] (
    [Id_City] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(100)  NOT NULL,
    [Id_State] int  NOT NULL,
    [Id_Country] int  NOT NULL
);
GO

-- Creating table 'Client'
CREATE TABLE [dbo].[Client] (
    [ID_Client] int IDENTITY(1,1) NOT NULL,
    [Id_Company] int  NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [CNPJ] varchar(50)  NULL,
    [Inscricao_Estadual] varchar(18)  NULL,
    [Inscricao_Municipal] varchar(50)  NULL,
    [CPF] varchar(50)  NULL,
    [RG] varchar(50)  NULL,
    [Id_ClientType] int  NULL,
    [Razao_Social] varchar(100)  NULL
);
GO

-- Creating table 'ClientAddress'
CREATE TABLE [dbo].[ClientAddress] (
    [Id_ClientAddress] int IDENTITY(1,1) NOT NULL,
    [Id_Client] int  NOT NULL,
    [Id_Address_Type] int  NOT NULL,
    [Street] varchar(100)  NULL,
    [Number] varchar(10)  NULL,
    [Complement] varchar(50)  NULL,
    [District] varchar(50)  NULL,
    [ZipCode] varchar(15)  NULL,
    [Id_City] int  NULL
);
GO

-- Creating table 'ClientContact'
CREATE TABLE [dbo].[ClientContact] (
    [Id_ClientContact] int IDENTITY(1,1) NOT NULL,
    [Id_Client] int  NOT NULL,
    [Name] varchar(100)  NOT NULL,
    [Department] varchar(50)  NOT NULL,
    [Phone_Comercial] varchar(50)  NULL,
    [Phone_Residence] varchar(50)  NULL,
    [Phone_Cell] varchar(50)  NULL,
    [Phone_Fax] varchar(50)  NULL,
    [Email] varchar(100)  NULL
);
GO

-- Creating table 'ClientType'
CREATE TABLE [dbo].[ClientType] (
    [ID_ClientType] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [Id_Company] int  NULL
);
GO

-- Creating table 'Company'
CREATE TABLE [dbo].[Company] (
    [ID_Company] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [CNPJ] varchar(50)  NULL,
    [Inscricao_Estadual] varchar(18)  NULL,
    [Inscricao_Municipal] varchar(50)  NULL,
    [CPF] varchar(50)  NULL,
    [RG] varchar(50)  NULL,
    [address] int  NULL,
    [phone] int  NULL,
    [email] int  NULL,
    [Logo_Url] varchar(50)  NULL,
    [Header_Url] varchar(50)  NULL
);
GO

-- Creating table 'CostCenter'
CREATE TABLE [dbo].[CostCenter] (
    [ID_CostCenter] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(20)  NOT NULL,
    [Id_Company] int  NULL
);
GO

-- Creating table 'CountParam'
CREATE TABLE [dbo].[CountParam] (
    [ID_CountParam] int IDENTITY(1,1) NOT NULL,
    [CountType] int  NOT NULL,
    [CountID] int  NOT NULL,
    [CountValue] int  NOT NULL
);
GO

-- Creating table 'Country'
CREATE TABLE [dbo].[Country] (
    [ID_Country] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL
);
GO

-- Creating table 'Debt'
CREATE TABLE [dbo].[Debt] (
    [ID_Debt] int IDENTITY(1,1) NOT NULL,
    [Id_Company] int  NOT NULL,
    [Id_Suplier] int  NOT NULL,
    [DocumentNumber] varchar(50)  NULL,
    [Description] varchar(50)  NULL,
    [DocumentDate] datetime  NOT NULL,
    [PaymentDate] datetime  NOT NULL,
    [ExpectedPaymentDate] datetime  NULL,
    [Id_CostCenter] int  NULL,
    [Id_Account] int  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'DebtParcels'
CREATE TABLE [dbo].[DebtParcels] (
    [ID_DebtParcels] int IDENTITY(1,1) NOT NULL,
    [Id_Debt] int  NOT NULL,
    [ParcelNumber] int  NOT NULL,
    [PaymentDate] datetime  NOT NULL,
    [ExpectedPaymentDate] datetime  NULL,
    [Id_BankTransaction] int  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'DebtPayment'
CREATE TABLE [dbo].[DebtPayment] (
    [ID_DebtPayment] int IDENTITY(1,1) NOT NULL,
    [Id_DebtParcel] int  NOT NULL,
    [Id_BankTransaction] int  NOT NULL,
    [Amount] decimal(19,4)  NULL,
    [Discount] decimal(19,4)  NULL,
    [Increase] decimal(19,4)  NULL,
    [PaidValue] decimal(19,4)  NULL
);
GO

-- Creating table 'DebtRecurring'
CREATE TABLE [dbo].[DebtRecurring] (
    [ID_DebtRecurring] int IDENTITY(1,1) NOT NULL,
    [Id_Suplier] int  NOT NULL,
    [DocumentNumber] varchar(50)  NULL,
    [Description] varchar(50)  NOT NULL,
    [Id_RecurringType] int  NOT NULL,
    [StartDate] datetime  NOT NULL,
    [FinalDate] datetime  NULL,
    [Id_CostCenter] int  NULL,
    [Id_Account] int  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'Invoice'
CREATE TABLE [dbo].[Invoice] (
    [ID_Invoice] int IDENTITY(1,1) NOT NULL,
    [Id_Company] int  NOT NULL,
    [Id_Order] int  NULL,
    [Id_Client] int  NULL,
    [Id_Seller] int  NULL,
    [OrderDate] datetime  NOT NULL,
    [InvoiceFile] varbinary(1)  NULL
);
GO

-- Creating table 'InvoiceItens'
CREATE TABLE [dbo].[InvoiceItens] (
    [ID_InvoiceItens] int IDENTITY(1,1) NOT NULL,
    [Id_Invoice] int  NOT NULL,
    [ItemNumber] int  NOT NULL,
    [Id_Product] int  NOT NULL,
    [Description] varchar(200)  NULL,
    [Id_UnitMeasurement] int  NOT NULL,
    [Quantity] decimal(18,5)  NULL,
    [UnitValue] decimal(19,4)  NULL,
    [TotalValue] decimal(19,4)  NULL
);
GO

-- Creating table 'InvoicePayments'
CREATE TABLE [dbo].[InvoicePayments] (
    [ID_InvoicePayments] int IDENTITY(1,1) NOT NULL,
    [Id_Invoice] int  NOT NULL,
    [PaymentDate] datetime  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'LogMessage'
CREATE TABLE [dbo].[LogMessage] (
    [ID_LogMessage] int IDENTITY(1,1) NOT NULL,
    [UserName] varchar(50)  NULL,
    [TaskName] varchar(50)  NULL,
    [DataHora] datetime  NULL,
    [Tipo] int  NULL,
    [Message] varchar(250)  NULL,
    [ObjectParam] int  NULL,
    [StackTrace] int  NULL,
    [ErrorMessage] varchar(500)  NULL
);
GO

-- Creating table 'Module'
CREATE TABLE [dbo].[Module] (
    [ID_Module] int IDENTITY(1,1) NOT NULL,
    [Id_ModuleGroup] int  NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [ModuleUrl] varchar(50)  NOT NULL
);
GO

-- Creating table 'ModuleGroup'
CREATE TABLE [dbo].[ModuleGroup] (
    [ID_ModuleGroup] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL
);
GO

-- Creating table 'OrderItens'
CREATE TABLE [dbo].[OrderItens] (
    [ID_OrderItens] int IDENTITY(1,1) NOT NULL,
    [Id_Order] int  NULL,
    [ItemNumber] int  NOT NULL,
    [Id_Product] int  NOT NULL,
    [Description] varchar(200)  NULL,
    [Id_UnitMeasurement] int  NOT NULL,
    [Quantity] decimal(18,5)  NULL,
    [UnitValue] decimal(19,4)  NULL,
    [TotalValue] decimal(19,4)  NULL
);
GO

-- Creating table 'OrderPayments'
CREATE TABLE [dbo].[OrderPayments] (
    [ID_OrderPayments] int IDENTITY(1,1) NOT NULL,
    [Id_Order] int  NOT NULL,
    [PaymentDate] datetime  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'Orders'
CREATE TABLE [dbo].[Orders] (
    [ID_Orders] int IDENTITY(1,1) NOT NULL,
    [Id_Company] int  NOT NULL,
    [Id_Proposal] int  NULL,
    [Id_Client] int  NULL,
    [Id_Seller] int  NULL,
    [OrderDate] datetime  NOT NULL,
    [OrderFile] varbinary(1)  NULL,
    [OrderStatus] int  NOT NULL
);
GO

-- Creating table 'PageLink'
CREATE TABLE [dbo].[PageLink] (
    [Id_PageLink] int IDENTITY(1,1) NOT NULL,
    [PageCode] char(4)  NULL,
    [Name] varchar(100)  NULL,
    [PageName] varchar(100)  NULL,
    [PageRequest] varchar(100)  NULL
);
GO

-- Creating table 'Phrase'
CREATE TABLE [dbo].[Phrase] (
    [Id_Phrase] int IDENTITY(1,1) NOT NULL,
    [PhraseText] varchar(1000)  NULL
);
GO

-- Creating table 'Product'
CREATE TABLE [dbo].[Product] (
    [ID_Product] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [Id_ProductType] int  NOT NULL,
    [Id_UnitMeasurement] int  NOT NULL,
    [NetWeight] decimal(18,0)  NULL,
    [GrossWeight] decimal(18,0)  NULL,
    [TechnicalCharacteristic1] varchar(50)  NULL,
    [TechnicalCharacteristic2] varchar(50)  NULL,
    [TechnicalCharacteristic3] varchar(50)  NULL,
    [TechnicalCharacteristic4] varchar(50)  NULL,
    [TechnicalCharacteristic5] varchar(50)  NULL,
    [TechnicalCharacteristic6] varchar(50)  NULL,
    [TechnicalCharacteristic7] varchar(50)  NULL,
    [TechnicalCharacteristic8] varchar(50)  NULL,
    [TechnicalCharacteristic9] varchar(50)  NULL,
    [Id_Company] int  NULL
);
GO

-- Creating table 'ProductType'
CREATE TABLE [dbo].[ProductType] (
    [ID_Product_Type] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [TechnicalField1] varchar(50)  NULL,
    [TechnicalField2] varchar(50)  NULL,
    [TechnicalField3] varchar(50)  NULL,
    [TechnicalField4] varchar(50)  NULL,
    [TechnicalField5] varchar(50)  NULL,
    [TechnicalField6] varchar(50)  NULL,
    [TechnicalField7] varchar(50)  NULL,
    [TechnicalField8] varchar(50)  NULL,
    [TechnicalField9] varchar(50)  NULL,
    [Id_Company] int  NULL
);
GO

-- Creating table 'Project'
CREATE TABLE [dbo].[Project] (
    [Id_Project] int IDENTITY(1,1) NOT NULL,
    [ProjectName] varchar(100)  NULL,
    [Id_Client] int  NOT NULL
);
GO

-- Creating table 'Proposal'
CREATE TABLE [dbo].[Proposal] (
    [ID_Proposal] int IDENTITY(1,1) NOT NULL,
    [Id_Company] int  NOT NULL,
    [Id_Client] int  NOT NULL,
    [Id_Seller] int  NULL,
    [ProposalDate] datetime  NOT NULL,
    [ProposalFile] varbinary(1)  NULL,
    [ProposalStatus] int  NOT NULL
);
GO

-- Creating table 'ProposalItens'
CREATE TABLE [dbo].[ProposalItens] (
    [ID_ProposalItens] int IDENTITY(1,1) NOT NULL,
    [Id_Proposal] int  NOT NULL,
    [ItemNumber] int  NULL,
    [Id_Product] int  NOT NULL,
    [Description] varchar(200)  NOT NULL,
    [Id_UnitMeasurement] int  NOT NULL,
    [Quantity] decimal(18,5)  NULL,
    [UnitValue] decimal(19,4)  NULL,
    [TotalValue] decimal(19,4)  NULL
);
GO

-- Creating table 'ProposalPayments'
CREATE TABLE [dbo].[ProposalPayments] (
    [ID_ProposalPayments] int IDENTITY(1,1) NOT NULL,
    [Id_Proposal] int  NOT NULL,
    [PaymentDate] datetime  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'Receivable'
CREATE TABLE [dbo].[Receivable] (
    [ID_Receivable] int IDENTITY(1,1) NOT NULL,
    [Id_Client] int  NOT NULL,
    [DocumentNumber] varchar(50)  NULL,
    [Description] varchar(50)  NOT NULL,
    [DocumentDate] datetime  NULL,
    [PaymentDate] datetime  NOT NULL,
    [ExpectedPaymentDate] datetime  NULL,
    [Id_CostCenter] int  NULL,
    [Id_Account] int  NOT NULL,
    [Id_BankTransaction] int  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'ReceivableParcels'
CREATE TABLE [dbo].[ReceivableParcels] (
    [ID_ReceivableParcels] int IDENTITY(1,1) NOT NULL,
    [Id_Receivable] int  NOT NULL,
    [ParcelNumber] int  NOT NULL,
    [PaymentDate] datetime  NOT NULL,
    [ExpectedPaymentDate] datetime  NULL,
    [Id_BankTransaction] int  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'ReceivablePayment'
CREATE TABLE [dbo].[ReceivablePayment] (
    [ID_ReceivablePayment] int IDENTITY(1,1) NOT NULL,
    [Id_ReceivableParcel] int  NOT NULL,
    [Id_BankTransaction] int  NOT NULL,
    [Amount] decimal(19,4)  NULL,
    [Discount] decimal(19,4)  NULL,
    [Increase] decimal(19,4)  NULL,
    [PaymentValue] decimal(19,4)  NULL
);
GO

-- Creating table 'ReceivableRecurring'
CREATE TABLE [dbo].[ReceivableRecurring] (
    [ID_ReceivableRecurring] int IDENTITY(1,1) NOT NULL,
    [Id_Client] int  NOT NULL,
    [DocumentNumber] varchar(50)  NULL,
    [Description] varchar(50)  NOT NULL,
    [Id_RecurringType] int  NOT NULL,
    [StartDate] datetime  NOT NULL,
    [FinalDate] datetime  NULL,
    [Id_CostCenter] int  NULL,
    [Id_Account] int  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'RecurringType'
CREATE TABLE [dbo].[RecurringType] (
    [ID_RecurringType] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [TypeRecurring] int  NOT NULL,
    [RecurringDay] varchar(50)  NOT NULL
);
GO

-- Creating table 'Rights'
CREATE TABLE [dbo].[Rights] (
    [ID_Rights] int IDENTITY(1,1) NOT NULL,
    [Id_SecurityGroup] int  NULL,
    [Id_Module] int  NOT NULL,
    [RightType] int  NOT NULL
);
GO

-- Creating table 'SecurityGroup'
CREATE TABLE [dbo].[SecurityGroup] (
    [ID_SecurityGroup] int IDENTITY(1,1) NOT NULL,
    [Id_Company] int  NOT NULL,
    [Name] varchar(50)  NOT NULL
);
GO

-- Creating table 'SecurityGroup_Users'
CREATE TABLE [dbo].[SecurityGroup_Users] (
    [ID_SecurityGroup_Users] int IDENTITY(1,1) NOT NULL,
    [Id_SecurityGroup] int  NOT NULL,
    [Id_Users] int  NOT NULL
);
GO

-- Creating table 'Seller'
CREATE TABLE [dbo].[Seller] (
    [ID_Seller] int IDENTITY(1,1) NOT NULL,
    [Id_Company] int  NOT NULL,
    [Name] varchar(50)  NOT NULL
);
GO

-- Creating table 'ServiceRequest'
CREATE TABLE [dbo].[ServiceRequest] (
    [Id_ServiceRequest] int IDENTITY(1,1) NOT NULL,
    [SR_Number] varchar(10)  NULL,
    [Name] varchar(50)  NULL,
    [SR_Description] varchar(500)  NULL,
    [Id_Project] int  NULL,
    [SR_Date] datetime  NOT NULL,
    [Id_User] int  NULL,
    [Status] varchar(10)  NULL
);
GO

-- Creating table 'ServiceRequest_Historic'
CREATE TABLE [dbo].[ServiceRequest_Historic] (
    [Id_ServiceRequestHistoric] int IDENTITY(1,1) NOT NULL,
    [Id_ServiceRequest] int  NOT NULL,
    [Historic_Date] datetime  NOT NULL,
    [Id_User] int  NOT NULL,
    [Status] varchar(10)  NOT NULL,
    [Observation] varchar(500)  NULL
);
GO

-- Creating table 'ShortCut'
CREATE TABLE [dbo].[ShortCut] (
    [Id_Shortcut] int IDENTITY(1,1) NOT NULL,
    [Id_PageLinkSource] int  NOT NULL,
    [Id_PageLink] int  NOT NULL
);
GO

-- Creating table 'State'
CREATE TABLE [dbo].[State] (
    [ID_State] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [Sigla] varchar(50)  NULL,
    [Id_Country] int  NOT NULL
);
GO

-- Creating table 'Stock'
CREATE TABLE [dbo].[Stock] (
    [ID_Stock] int IDENTITY(1,1) NOT NULL,
    [Id_Product] int  NOT NULL,
    [Quantity] decimal(18,5)  NULL
);
GO

-- Creating table 'StockTransaction'
CREATE TABLE [dbo].[StockTransaction] (
    [ID_StockTransaction] int IDENTITY(1,1) NOT NULL,
    [Id_Product] int  NOT NULL,
    [DocumentNumber] varchar(50)  NULL,
    [Description] varchar(50)  NULL,
    [TransactionDate] datetime  NOT NULL,
    [Id_TransactionType] int  NOT NULL,
    [Quantity] decimal(18,0)  NOT NULL,
    [Amount] decimal(19,4)  NULL
);
GO

-- Creating table 'Supplier'
CREATE TABLE [dbo].[Supplier] (
    [ID_Supplier] int IDENTITY(1,1) NOT NULL,
    [Id_Company] int  NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [CNPJ] varchar(50)  NULL,
    [CPF] varchar(50)  NULL,
    [RG] varchar(50)  NULL,
    [Id_SupplierType] int  NOT NULL,
    [Razao_Social] varchar(100)  NULL,
    [Inscricao_Municipal] varchar(50)  NULL,
    [Inscricao_Estadual] varchar(18)  NULL
);
GO

-- Creating table 'SupplierAddress'
CREATE TABLE [dbo].[SupplierAddress] (
    [Id_SupplierAddress] int IDENTITY(1,1) NOT NULL,
    [Id_Supplier] int  NOT NULL,
    [Id_Address_Type] int  NOT NULL,
    [Street] varchar(100)  NULL,
    [Number] varchar(10)  NULL,
    [Complement] varchar(50)  NULL,
    [District] varchar(50)  NULL,
    [ZipCode] varchar(15)  NULL,
    [Id_City] int  NULL
);
GO

-- Creating table 'SupplierContact'
CREATE TABLE [dbo].[SupplierContact] (
    [Id_SupplierContact] int IDENTITY(1,1) NOT NULL,
    [Id_Supplier] int  NOT NULL,
    [Name] varchar(100)  NOT NULL,
    [Department] varchar(50)  NOT NULL,
    [Phone_Comercial] varchar(50)  NULL,
    [Phone_Residence] varchar(50)  NULL,
    [Phone_Cell] varchar(50)  NULL,
    [Phone_Fax] varchar(50)  NULL,
    [Email] varchar(100)  NULL
);
GO

-- Creating table 'SupplierType'
CREATE TABLE [dbo].[SupplierType] (
    [ID_SupplierType] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [Id_Company] int  NULL
);
GO

-- Creating table 'Teste'
CREATE TABLE [dbo].[Teste] (
    [Id_Teste] int IDENTITY(1,1) NOT NULL,
    [Nome1] varchar(100)  NULL,
    [Nome2] varchar(100)  NULL
);
GO

-- Creating table 'TimeSheet'
CREATE TABLE [dbo].[TimeSheet] (
    [Id_TimeSheet] int IDENTITY(1,1) NOT NULL,
    [Id_User] int  NOT NULL,
    [Id_TroubleTicket] int  NULL,
    [Id_ServiceRequest] int  NULL,
    [StartDate] datetime  NOT NULL,
    [FinalDate] datetime  NULL
);
GO

-- Creating table 'TransactionType'
CREATE TABLE [dbo].[TransactionType] (
    [ID_TransactionType] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL,
    [Type] int  NOT NULL
);
GO

-- Creating table 'TroubleTicket'
CREATE TABLE [dbo].[TroubleTicket] (
    [Id_TroubleTicket] int IDENTITY(1,1) NOT NULL,
    [TR_Number] varchar(10)  NULL,
    [Name] varchar(50)  NULL,
    [TR_Description] varchar(500)  NULL,
    [Id_Project] int  NULL,
    [TR_Date] datetime  NOT NULL,
    [Id_User] int  NULL,
    [Status] varchar(10)  NULL
);
GO

-- Creating table 'TroubleTicket_Historic'
CREATE TABLE [dbo].[TroubleTicket_Historic] (
    [Id_TroubleTicketHistoric] int IDENTITY(1,1) NOT NULL,
    [Id_TroubleTicket] int  NOT NULL,
    [Historic_Date] datetime  NOT NULL,
    [Id_User] int  NOT NULL,
    [Status] varchar(10)  NOT NULL,
    [Observation] varchar(500)  NULL
);
GO

-- Creating table 'UnitMeasurement'
CREATE TABLE [dbo].[UnitMeasurement] (
    [ID_UnitMeasurement] int IDENTITY(1,1) NOT NULL,
    [Name] varchar(50)  NOT NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [ID_Users] int IDENTITY(1,1) NOT NULL,
    [UserName] varchar(50)  NOT NULL,
    [Id_CostCenter] int  NOT NULL,
    [Login] varchar(50)  NOT NULL,
    [Password] varchar(50)  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [ID_Account] in table 'Account'
ALTER TABLE [dbo].[Account]
ADD CONSTRAINT [PK_Account]
    PRIMARY KEY CLUSTERED ([ID_Account] ASC);
GO

-- Creating primary key on [Id_Address_Type] in table 'Address_Type'
ALTER TABLE [dbo].[Address_Type]
ADD CONSTRAINT [PK_Address_Type]
    PRIMARY KEY CLUSTERED ([Id_Address_Type] ASC);
GO

-- Creating primary key on [ID_Bank] in table 'Bank'
ALTER TABLE [dbo].[Bank]
ADD CONSTRAINT [PK_Bank]
    PRIMARY KEY CLUSTERED ([ID_Bank] ASC);
GO

-- Creating primary key on [ID_BankAccount] in table 'BankAccount'
ALTER TABLE [dbo].[BankAccount]
ADD CONSTRAINT [PK_BankAccount]
    PRIMARY KEY CLUSTERED ([ID_BankAccount] ASC);
GO

-- Creating primary key on [Id_BankAccount], [BalanceDate] in table 'BankAccountBalance'
ALTER TABLE [dbo].[BankAccountBalance]
ADD CONSTRAINT [PK_BankAccountBalance]
    PRIMARY KEY CLUSTERED ([Id_BankAccount], [BalanceDate] ASC);
GO

-- Creating primary key on [Id_Bank] in table 'BankCheckLayout'
ALTER TABLE [dbo].[BankCheckLayout]
ADD CONSTRAINT [PK_BankCheckLayout]
    PRIMARY KEY CLUSTERED ([Id_Bank] ASC);
GO

-- Creating primary key on [ID_BankTransaction] in table 'BankTransaction'
ALTER TABLE [dbo].[BankTransaction]
ADD CONSTRAINT [PK_BankTransaction]
    PRIMARY KEY CLUSTERED ([ID_BankTransaction] ASC);
GO

-- Creating primary key on [Id_City] in table 'City'
ALTER TABLE [dbo].[City]
ADD CONSTRAINT [PK_City]
    PRIMARY KEY CLUSTERED ([Id_City] ASC);
GO

-- Creating primary key on [ID_Client] in table 'Client'
ALTER TABLE [dbo].[Client]
ADD CONSTRAINT [PK_Client]
    PRIMARY KEY CLUSTERED ([ID_Client] ASC);
GO

-- Creating primary key on [Id_ClientAddress] in table 'ClientAddress'
ALTER TABLE [dbo].[ClientAddress]
ADD CONSTRAINT [PK_ClientAddress]
    PRIMARY KEY CLUSTERED ([Id_ClientAddress] ASC);
GO

-- Creating primary key on [Id_ClientContact] in table 'ClientContact'
ALTER TABLE [dbo].[ClientContact]
ADD CONSTRAINT [PK_ClientContact]
    PRIMARY KEY CLUSTERED ([Id_ClientContact] ASC);
GO

-- Creating primary key on [ID_ClientType] in table 'ClientType'
ALTER TABLE [dbo].[ClientType]
ADD CONSTRAINT [PK_ClientType]
    PRIMARY KEY CLUSTERED ([ID_ClientType] ASC);
GO

-- Creating primary key on [ID_Company] in table 'Company'
ALTER TABLE [dbo].[Company]
ADD CONSTRAINT [PK_Company]
    PRIMARY KEY CLUSTERED ([ID_Company] ASC);
GO

-- Creating primary key on [ID_CostCenter] in table 'CostCenter'
ALTER TABLE [dbo].[CostCenter]
ADD CONSTRAINT [PK_CostCenter]
    PRIMARY KEY CLUSTERED ([ID_CostCenter] ASC);
GO

-- Creating primary key on [ID_CountParam] in table 'CountParam'
ALTER TABLE [dbo].[CountParam]
ADD CONSTRAINT [PK_CountParam]
    PRIMARY KEY CLUSTERED ([ID_CountParam] ASC);
GO

-- Creating primary key on [ID_Country] in table 'Country'
ALTER TABLE [dbo].[Country]
ADD CONSTRAINT [PK_Country]
    PRIMARY KEY CLUSTERED ([ID_Country] ASC);
GO

-- Creating primary key on [ID_Debt] in table 'Debt'
ALTER TABLE [dbo].[Debt]
ADD CONSTRAINT [PK_Debt]
    PRIMARY KEY CLUSTERED ([ID_Debt] ASC);
GO

-- Creating primary key on [ID_DebtParcels] in table 'DebtParcels'
ALTER TABLE [dbo].[DebtParcels]
ADD CONSTRAINT [PK_DebtParcels]
    PRIMARY KEY CLUSTERED ([ID_DebtParcels] ASC);
GO

-- Creating primary key on [ID_DebtPayment] in table 'DebtPayment'
ALTER TABLE [dbo].[DebtPayment]
ADD CONSTRAINT [PK_DebtPayment]
    PRIMARY KEY CLUSTERED ([ID_DebtPayment] ASC);
GO

-- Creating primary key on [ID_DebtRecurring] in table 'DebtRecurring'
ALTER TABLE [dbo].[DebtRecurring]
ADD CONSTRAINT [PK_DebtRecurring]
    PRIMARY KEY CLUSTERED ([ID_DebtRecurring] ASC);
GO

-- Creating primary key on [ID_Invoice] in table 'Invoice'
ALTER TABLE [dbo].[Invoice]
ADD CONSTRAINT [PK_Invoice]
    PRIMARY KEY CLUSTERED ([ID_Invoice] ASC);
GO

-- Creating primary key on [ID_InvoiceItens] in table 'InvoiceItens'
ALTER TABLE [dbo].[InvoiceItens]
ADD CONSTRAINT [PK_InvoiceItens]
    PRIMARY KEY CLUSTERED ([ID_InvoiceItens] ASC);
GO

-- Creating primary key on [ID_InvoicePayments] in table 'InvoicePayments'
ALTER TABLE [dbo].[InvoicePayments]
ADD CONSTRAINT [PK_InvoicePayments]
    PRIMARY KEY CLUSTERED ([ID_InvoicePayments] ASC);
GO

-- Creating primary key on [ID_LogMessage] in table 'LogMessage'
ALTER TABLE [dbo].[LogMessage]
ADD CONSTRAINT [PK_LogMessage]
    PRIMARY KEY CLUSTERED ([ID_LogMessage] ASC);
GO

-- Creating primary key on [ID_Module] in table 'Module'
ALTER TABLE [dbo].[Module]
ADD CONSTRAINT [PK_Module]
    PRIMARY KEY CLUSTERED ([ID_Module] ASC);
GO

-- Creating primary key on [ID_ModuleGroup] in table 'ModuleGroup'
ALTER TABLE [dbo].[ModuleGroup]
ADD CONSTRAINT [PK_ModuleGroup]
    PRIMARY KEY CLUSTERED ([ID_ModuleGroup] ASC);
GO

-- Creating primary key on [ID_OrderItens] in table 'OrderItens'
ALTER TABLE [dbo].[OrderItens]
ADD CONSTRAINT [PK_OrderItens]
    PRIMARY KEY CLUSTERED ([ID_OrderItens] ASC);
GO

-- Creating primary key on [ID_OrderPayments] in table 'OrderPayments'
ALTER TABLE [dbo].[OrderPayments]
ADD CONSTRAINT [PK_OrderPayments]
    PRIMARY KEY CLUSTERED ([ID_OrderPayments] ASC);
GO

-- Creating primary key on [ID_Orders] in table 'Orders'
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [PK_Orders]
    PRIMARY KEY CLUSTERED ([ID_Orders] ASC);
GO

-- Creating primary key on [Id_PageLink] in table 'PageLink'
ALTER TABLE [dbo].[PageLink]
ADD CONSTRAINT [PK_PageLink]
    PRIMARY KEY CLUSTERED ([Id_PageLink] ASC);
GO

-- Creating primary key on [Id_Phrase] in table 'Phrase'
ALTER TABLE [dbo].[Phrase]
ADD CONSTRAINT [PK_Phrase]
    PRIMARY KEY CLUSTERED ([Id_Phrase] ASC);
GO

-- Creating primary key on [ID_Product] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [PK_Product]
    PRIMARY KEY CLUSTERED ([ID_Product] ASC);
GO

-- Creating primary key on [ID_Product_Type] in table 'ProductType'
ALTER TABLE [dbo].[ProductType]
ADD CONSTRAINT [PK_ProductType]
    PRIMARY KEY CLUSTERED ([ID_Product_Type] ASC);
GO

-- Creating primary key on [Id_Project] in table 'Project'
ALTER TABLE [dbo].[Project]
ADD CONSTRAINT [PK_Project]
    PRIMARY KEY CLUSTERED ([Id_Project] ASC);
GO

-- Creating primary key on [ID_Proposal] in table 'Proposal'
ALTER TABLE [dbo].[Proposal]
ADD CONSTRAINT [PK_Proposal]
    PRIMARY KEY CLUSTERED ([ID_Proposal] ASC);
GO

-- Creating primary key on [ID_ProposalItens] in table 'ProposalItens'
ALTER TABLE [dbo].[ProposalItens]
ADD CONSTRAINT [PK_ProposalItens]
    PRIMARY KEY CLUSTERED ([ID_ProposalItens] ASC);
GO

-- Creating primary key on [ID_ProposalPayments] in table 'ProposalPayments'
ALTER TABLE [dbo].[ProposalPayments]
ADD CONSTRAINT [PK_ProposalPayments]
    PRIMARY KEY CLUSTERED ([ID_ProposalPayments] ASC);
GO

-- Creating primary key on [ID_Receivable] in table 'Receivable'
ALTER TABLE [dbo].[Receivable]
ADD CONSTRAINT [PK_Receivable]
    PRIMARY KEY CLUSTERED ([ID_Receivable] ASC);
GO

-- Creating primary key on [ID_ReceivableParcels] in table 'ReceivableParcels'
ALTER TABLE [dbo].[ReceivableParcels]
ADD CONSTRAINT [PK_ReceivableParcels]
    PRIMARY KEY CLUSTERED ([ID_ReceivableParcels] ASC);
GO

-- Creating primary key on [ID_ReceivablePayment] in table 'ReceivablePayment'
ALTER TABLE [dbo].[ReceivablePayment]
ADD CONSTRAINT [PK_ReceivablePayment]
    PRIMARY KEY CLUSTERED ([ID_ReceivablePayment] ASC);
GO

-- Creating primary key on [ID_ReceivableRecurring] in table 'ReceivableRecurring'
ALTER TABLE [dbo].[ReceivableRecurring]
ADD CONSTRAINT [PK_ReceivableRecurring]
    PRIMARY KEY CLUSTERED ([ID_ReceivableRecurring] ASC);
GO

-- Creating primary key on [ID_RecurringType] in table 'RecurringType'
ALTER TABLE [dbo].[RecurringType]
ADD CONSTRAINT [PK_RecurringType]
    PRIMARY KEY CLUSTERED ([ID_RecurringType] ASC);
GO

-- Creating primary key on [ID_Rights] in table 'Rights'
ALTER TABLE [dbo].[Rights]
ADD CONSTRAINT [PK_Rights]
    PRIMARY KEY CLUSTERED ([ID_Rights] ASC);
GO

-- Creating primary key on [ID_SecurityGroup] in table 'SecurityGroup'
ALTER TABLE [dbo].[SecurityGroup]
ADD CONSTRAINT [PK_SecurityGroup]
    PRIMARY KEY CLUSTERED ([ID_SecurityGroup] ASC);
GO

-- Creating primary key on [ID_SecurityGroup_Users] in table 'SecurityGroup_Users'
ALTER TABLE [dbo].[SecurityGroup_Users]
ADD CONSTRAINT [PK_SecurityGroup_Users]
    PRIMARY KEY CLUSTERED ([ID_SecurityGroup_Users] ASC);
GO

-- Creating primary key on [ID_Seller] in table 'Seller'
ALTER TABLE [dbo].[Seller]
ADD CONSTRAINT [PK_Seller]
    PRIMARY KEY CLUSTERED ([ID_Seller] ASC);
GO

-- Creating primary key on [Id_ServiceRequest] in table 'ServiceRequest'
ALTER TABLE [dbo].[ServiceRequest]
ADD CONSTRAINT [PK_ServiceRequest]
    PRIMARY KEY CLUSTERED ([Id_ServiceRequest] ASC);
GO

-- Creating primary key on [Id_ServiceRequestHistoric] in table 'ServiceRequest_Historic'
ALTER TABLE [dbo].[ServiceRequest_Historic]
ADD CONSTRAINT [PK_ServiceRequest_Historic]
    PRIMARY KEY CLUSTERED ([Id_ServiceRequestHistoric] ASC);
GO

-- Creating primary key on [Id_Shortcut] in table 'ShortCut'
ALTER TABLE [dbo].[ShortCut]
ADD CONSTRAINT [PK_ShortCut]
    PRIMARY KEY CLUSTERED ([Id_Shortcut] ASC);
GO

-- Creating primary key on [ID_State] in table 'State'
ALTER TABLE [dbo].[State]
ADD CONSTRAINT [PK_State]
    PRIMARY KEY CLUSTERED ([ID_State] ASC);
GO

-- Creating primary key on [ID_Stock] in table 'Stock'
ALTER TABLE [dbo].[Stock]
ADD CONSTRAINT [PK_Stock]
    PRIMARY KEY CLUSTERED ([ID_Stock] ASC);
GO

-- Creating primary key on [ID_StockTransaction] in table 'StockTransaction'
ALTER TABLE [dbo].[StockTransaction]
ADD CONSTRAINT [PK_StockTransaction]
    PRIMARY KEY CLUSTERED ([ID_StockTransaction] ASC);
GO

-- Creating primary key on [ID_Supplier] in table 'Supplier'
ALTER TABLE [dbo].[Supplier]
ADD CONSTRAINT [PK_Supplier]
    PRIMARY KEY CLUSTERED ([ID_Supplier] ASC);
GO

-- Creating primary key on [Id_SupplierAddress] in table 'SupplierAddress'
ALTER TABLE [dbo].[SupplierAddress]
ADD CONSTRAINT [PK_SupplierAddress]
    PRIMARY KEY CLUSTERED ([Id_SupplierAddress] ASC);
GO

-- Creating primary key on [Id_SupplierContact] in table 'SupplierContact'
ALTER TABLE [dbo].[SupplierContact]
ADD CONSTRAINT [PK_SupplierContact]
    PRIMARY KEY CLUSTERED ([Id_SupplierContact] ASC);
GO

-- Creating primary key on [ID_SupplierType] in table 'SupplierType'
ALTER TABLE [dbo].[SupplierType]
ADD CONSTRAINT [PK_SupplierType]
    PRIMARY KEY CLUSTERED ([ID_SupplierType] ASC);
GO

-- Creating primary key on [Id_Teste] in table 'Teste'
ALTER TABLE [dbo].[Teste]
ADD CONSTRAINT [PK_Teste]
    PRIMARY KEY CLUSTERED ([Id_Teste] ASC);
GO

-- Creating primary key on [Id_TimeSheet] in table 'TimeSheet'
ALTER TABLE [dbo].[TimeSheet]
ADD CONSTRAINT [PK_TimeSheet]
    PRIMARY KEY CLUSTERED ([Id_TimeSheet] ASC);
GO

-- Creating primary key on [ID_TransactionType] in table 'TransactionType'
ALTER TABLE [dbo].[TransactionType]
ADD CONSTRAINT [PK_TransactionType]
    PRIMARY KEY CLUSTERED ([ID_TransactionType] ASC);
GO

-- Creating primary key on [Id_TroubleTicket] in table 'TroubleTicket'
ALTER TABLE [dbo].[TroubleTicket]
ADD CONSTRAINT [PK_TroubleTicket]
    PRIMARY KEY CLUSTERED ([Id_TroubleTicket] ASC);
GO

-- Creating primary key on [Id_TroubleTicketHistoric] in table 'TroubleTicket_Historic'
ALTER TABLE [dbo].[TroubleTicket_Historic]
ADD CONSTRAINT [PK_TroubleTicket_Historic]
    PRIMARY KEY CLUSTERED ([Id_TroubleTicketHistoric] ASC);
GO

-- Creating primary key on [ID_UnitMeasurement] in table 'UnitMeasurement'
ALTER TABLE [dbo].[UnitMeasurement]
ADD CONSTRAINT [PK_UnitMeasurement]
    PRIMARY KEY CLUSTERED ([ID_UnitMeasurement] ASC);
GO

-- Creating primary key on [ID_Users] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([ID_Users] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [Id_ParentAccount] in table 'Account'
ALTER TABLE [dbo].[Account]
ADD CONSTRAINT [FK_Account_Account]
    FOREIGN KEY ([Id_ParentAccount])
    REFERENCES [dbo].[Account]
        ([ID_Account])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Account_Account'
CREATE INDEX [IX_FK_Account_Account]
ON [dbo].[Account]
    ([Id_ParentAccount]);
GO

-- Creating foreign key on [Id_Company] in table 'Account'
ALTER TABLE [dbo].[Account]
ADD CONSTRAINT [FK_Account_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Account_Company'
CREATE INDEX [IX_FK_Account_Company]
ON [dbo].[Account]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Account] in table 'BankTransaction'
ALTER TABLE [dbo].[BankTransaction]
ADD CONSTRAINT [FK_BankTransaction_Account]
    FOREIGN KEY ([Id_Account])
    REFERENCES [dbo].[Account]
        ([ID_Account])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BankTransaction_Account'
CREATE INDEX [IX_FK_BankTransaction_Account]
ON [dbo].[BankTransaction]
    ([Id_Account]);
GO

-- Creating foreign key on [Id_Account] in table 'Debt'
ALTER TABLE [dbo].[Debt]
ADD CONSTRAINT [FK_Debt_Account]
    FOREIGN KEY ([Id_Account])
    REFERENCES [dbo].[Account]
        ([ID_Account])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Debt_Account'
CREATE INDEX [IX_FK_Debt_Account]
ON [dbo].[Debt]
    ([Id_Account]);
GO

-- Creating foreign key on [Id_Account] in table 'DebtRecurring'
ALTER TABLE [dbo].[DebtRecurring]
ADD CONSTRAINT [FK_DebtRecurring_Account]
    FOREIGN KEY ([Id_Account])
    REFERENCES [dbo].[Account]
        ([ID_Account])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DebtRecurring_Account'
CREATE INDEX [IX_FK_DebtRecurring_Account]
ON [dbo].[DebtRecurring]
    ([Id_Account]);
GO

-- Creating foreign key on [Id_Account] in table 'Receivable'
ALTER TABLE [dbo].[Receivable]
ADD CONSTRAINT [FK_Receivable_Account]
    FOREIGN KEY ([Id_Account])
    REFERENCES [dbo].[Account]
        ([ID_Account])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Receivable_Account'
CREATE INDEX [IX_FK_Receivable_Account]
ON [dbo].[Receivable]
    ([Id_Account]);
GO

-- Creating foreign key on [Id_Account] in table 'ReceivableRecurring'
ALTER TABLE [dbo].[ReceivableRecurring]
ADD CONSTRAINT [FK_ReceivableRecurring_Account]
    FOREIGN KEY ([Id_Account])
    REFERENCES [dbo].[Account]
        ([ID_Account])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReceivableRecurring_Account'
CREATE INDEX [IX_FK_ReceivableRecurring_Account]
ON [dbo].[ReceivableRecurring]
    ([Id_Account]);
GO

-- Creating foreign key on [Id_Address_Type] in table 'ClientAddress'
ALTER TABLE [dbo].[ClientAddress]
ADD CONSTRAINT [FK_ClientAdress_Address_Type]
    FOREIGN KEY ([Id_Address_Type])
    REFERENCES [dbo].[Address_Type]
        ([Id_Address_Type])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ClientAdress_Address_Type'
CREATE INDEX [IX_FK_ClientAdress_Address_Type]
ON [dbo].[ClientAddress]
    ([Id_Address_Type]);
GO

-- Creating foreign key on [Id_Address_Type] in table 'SupplierAddress'
ALTER TABLE [dbo].[SupplierAddress]
ADD CONSTRAINT [FK_SupplierAdress_Address_Type]
    FOREIGN KEY ([Id_Address_Type])
    REFERENCES [dbo].[Address_Type]
        ([Id_Address_Type])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SupplierAdress_Address_Type'
CREATE INDEX [IX_FK_SupplierAdress_Address_Type]
ON [dbo].[SupplierAddress]
    ([Id_Address_Type]);
GO

-- Creating foreign key on [Id_Company] in table 'Bank'
ALTER TABLE [dbo].[Bank]
ADD CONSTRAINT [FK_Bank_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Bank_Company'
CREATE INDEX [IX_FK_Bank_Company]
ON [dbo].[Bank]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Bank] in table 'BankAccount'
ALTER TABLE [dbo].[BankAccount]
ADD CONSTRAINT [FK_BankAccount_Bank]
    FOREIGN KEY ([Id_Bank])
    REFERENCES [dbo].[Bank]
        ([ID_Bank])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BankAccount_Bank'
CREATE INDEX [IX_FK_BankAccount_Bank]
ON [dbo].[BankAccount]
    ([Id_Bank]);
GO

-- Creating foreign key on [Id_Bank] in table 'BankCheckLayout'
ALTER TABLE [dbo].[BankCheckLayout]
ADD CONSTRAINT [FK_BankCheckLayout_Bank]
    FOREIGN KEY ([Id_Bank])
    REFERENCES [dbo].[Bank]
        ([ID_Bank])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Id_BankAccount] in table 'BankTransaction'
ALTER TABLE [dbo].[BankTransaction]
ADD CONSTRAINT [FK_BankTransaction_BankAccount]
    FOREIGN KEY ([Id_BankAccount])
    REFERENCES [dbo].[BankAccount]
        ([ID_BankAccount])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BankTransaction_BankAccount'
CREATE INDEX [IX_FK_BankTransaction_BankAccount]
ON [dbo].[BankTransaction]
    ([Id_BankAccount]);
GO

-- Creating foreign key on [Id_Client] in table 'BankTransaction'
ALTER TABLE [dbo].[BankTransaction]
ADD CONSTRAINT [FK_BankTransaction_Client]
    FOREIGN KEY ([Id_Client])
    REFERENCES [dbo].[Client]
        ([ID_Client])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BankTransaction_Client'
CREATE INDEX [IX_FK_BankTransaction_Client]
ON [dbo].[BankTransaction]
    ([Id_Client]);
GO

-- Creating foreign key on [Id_CostCenter] in table 'BankTransaction'
ALTER TABLE [dbo].[BankTransaction]
ADD CONSTRAINT [FK_BankTransaction_CostCenter]
    FOREIGN KEY ([Id_CostCenter])
    REFERENCES [dbo].[CostCenter]
        ([ID_CostCenter])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BankTransaction_CostCenter'
CREATE INDEX [IX_FK_BankTransaction_CostCenter]
ON [dbo].[BankTransaction]
    ([Id_CostCenter]);
GO

-- Creating foreign key on [Id_Supplier] in table 'BankTransaction'
ALTER TABLE [dbo].[BankTransaction]
ADD CONSTRAINT [FK_BankTransaction_Supplier]
    FOREIGN KEY ([Id_Supplier])
    REFERENCES [dbo].[Supplier]
        ([ID_Supplier])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_BankTransaction_Supplier'
CREATE INDEX [IX_FK_BankTransaction_Supplier]
ON [dbo].[BankTransaction]
    ([Id_Supplier]);
GO

-- Creating foreign key on [Id_BankTransaction] in table 'DebtParcels'
ALTER TABLE [dbo].[DebtParcels]
ADD CONSTRAINT [FK_DebtParcels_BankTransaction]
    FOREIGN KEY ([Id_BankTransaction])
    REFERENCES [dbo].[BankTransaction]
        ([ID_BankTransaction])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DebtParcels_BankTransaction'
CREATE INDEX [IX_FK_DebtParcels_BankTransaction]
ON [dbo].[DebtParcels]
    ([Id_BankTransaction]);
GO

-- Creating foreign key on [Id_BankTransaction] in table 'DebtPayment'
ALTER TABLE [dbo].[DebtPayment]
ADD CONSTRAINT [FK_DebtPayment_BankTransaction]
    FOREIGN KEY ([Id_BankTransaction])
    REFERENCES [dbo].[BankTransaction]
        ([ID_BankTransaction])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DebtPayment_BankTransaction'
CREATE INDEX [IX_FK_DebtPayment_BankTransaction]
ON [dbo].[DebtPayment]
    ([Id_BankTransaction]);
GO

-- Creating foreign key on [Id_BankTransaction] in table 'Receivable'
ALTER TABLE [dbo].[Receivable]
ADD CONSTRAINT [FK_Receivable_BankTransaction]
    FOREIGN KEY ([Id_BankTransaction])
    REFERENCES [dbo].[BankTransaction]
        ([ID_BankTransaction])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Receivable_BankTransaction'
CREATE INDEX [IX_FK_Receivable_BankTransaction]
ON [dbo].[Receivable]
    ([Id_BankTransaction]);
GO

-- Creating foreign key on [Id_BankTransaction] in table 'ReceivableParcels'
ALTER TABLE [dbo].[ReceivableParcels]
ADD CONSTRAINT [FK_ReceivableParcels_BankTransaction]
    FOREIGN KEY ([Id_BankTransaction])
    REFERENCES [dbo].[BankTransaction]
        ([ID_BankTransaction])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReceivableParcels_BankTransaction'
CREATE INDEX [IX_FK_ReceivableParcels_BankTransaction]
ON [dbo].[ReceivableParcels]
    ([Id_BankTransaction]);
GO

-- Creating foreign key on [Id_BankTransaction] in table 'ReceivablePayment'
ALTER TABLE [dbo].[ReceivablePayment]
ADD CONSTRAINT [FK_ReceivablePayment_BankTransaction]
    FOREIGN KEY ([Id_BankTransaction])
    REFERENCES [dbo].[BankTransaction]
        ([ID_BankTransaction])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReceivablePayment_BankTransaction'
CREATE INDEX [IX_FK_ReceivablePayment_BankTransaction]
ON [dbo].[ReceivablePayment]
    ([Id_BankTransaction]);
GO

-- Creating foreign key on [Id_Country] in table 'City'
ALTER TABLE [dbo].[City]
ADD CONSTRAINT [FK_City_Country]
    FOREIGN KEY ([Id_Country])
    REFERENCES [dbo].[Country]
        ([ID_Country])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_City_Country'
CREATE INDEX [IX_FK_City_Country]
ON [dbo].[City]
    ([Id_Country]);
GO

-- Creating foreign key on [Id_State] in table 'City'
ALTER TABLE [dbo].[City]
ADD CONSTRAINT [FK_City_State]
    FOREIGN KEY ([Id_State])
    REFERENCES [dbo].[State]
        ([ID_State])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_City_State'
CREATE INDEX [IX_FK_City_State]
ON [dbo].[City]
    ([Id_State]);
GO

-- Creating foreign key on [Id_City] in table 'ClientAddress'
ALTER TABLE [dbo].[ClientAddress]
ADD CONSTRAINT [FK_ClientAdress_City]
    FOREIGN KEY ([Id_City])
    REFERENCES [dbo].[City]
        ([Id_City])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ClientAdress_City'
CREATE INDEX [IX_FK_ClientAdress_City]
ON [dbo].[ClientAddress]
    ([Id_City]);
GO

-- Creating foreign key on [Id_City] in table 'SupplierAddress'
ALTER TABLE [dbo].[SupplierAddress]
ADD CONSTRAINT [FK_SupplierAdress_City]
    FOREIGN KEY ([Id_City])
    REFERENCES [dbo].[City]
        ([Id_City])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SupplierAdress_City'
CREATE INDEX [IX_FK_SupplierAdress_City]
ON [dbo].[SupplierAddress]
    ([Id_City]);
GO

-- Creating foreign key on [Id_ClientType] in table 'Client'
ALTER TABLE [dbo].[Client]
ADD CONSTRAINT [FK_Client_ClientType]
    FOREIGN KEY ([Id_ClientType])
    REFERENCES [dbo].[ClientType]
        ([ID_ClientType])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Client_ClientType'
CREATE INDEX [IX_FK_Client_ClientType]
ON [dbo].[Client]
    ([Id_ClientType]);
GO

-- Creating foreign key on [Id_Company] in table 'Client'
ALTER TABLE [dbo].[Client]
ADD CONSTRAINT [FK_Client_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Client_Company'
CREATE INDEX [IX_FK_Client_Company]
ON [dbo].[Client]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Client] in table 'ClientAddress'
ALTER TABLE [dbo].[ClientAddress]
ADD CONSTRAINT [FK_ClientAdress_Client]
    FOREIGN KEY ([Id_Client])
    REFERENCES [dbo].[Client]
        ([ID_Client])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ClientAdress_Client'
CREATE INDEX [IX_FK_ClientAdress_Client]
ON [dbo].[ClientAddress]
    ([Id_Client]);
GO

-- Creating foreign key on [Id_Client] in table 'ClientContact'
ALTER TABLE [dbo].[ClientContact]
ADD CONSTRAINT [FK_ClientContact_Client]
    FOREIGN KEY ([Id_Client])
    REFERENCES [dbo].[Client]
        ([ID_Client])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ClientContact_Client'
CREATE INDEX [IX_FK_ClientContact_Client]
ON [dbo].[ClientContact]
    ([Id_Client]);
GO

-- Creating foreign key on [Id_Client] in table 'Invoice'
ALTER TABLE [dbo].[Invoice]
ADD CONSTRAINT [FK_Invoice_Client]
    FOREIGN KEY ([Id_Client])
    REFERENCES [dbo].[Client]
        ([ID_Client])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Invoice_Client'
CREATE INDEX [IX_FK_Invoice_Client]
ON [dbo].[Invoice]
    ([Id_Client]);
GO

-- Creating foreign key on [Id_Client] in table 'Orders'
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_Orders_Client]
    FOREIGN KEY ([Id_Client])
    REFERENCES [dbo].[Client]
        ([ID_Client])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Orders_Client'
CREATE INDEX [IX_FK_Orders_Client]
ON [dbo].[Orders]
    ([Id_Client]);
GO

-- Creating foreign key on [Id_Client] in table 'Project'
ALTER TABLE [dbo].[Project]
ADD CONSTRAINT [FK_Project_Client]
    FOREIGN KEY ([Id_Client])
    REFERENCES [dbo].[Client]
        ([ID_Client])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Project_Client'
CREATE INDEX [IX_FK_Project_Client]
ON [dbo].[Project]
    ([Id_Client]);
GO

-- Creating foreign key on [Id_Client] in table 'Proposal'
ALTER TABLE [dbo].[Proposal]
ADD CONSTRAINT [FK_Proposal_Client]
    FOREIGN KEY ([Id_Client])
    REFERENCES [dbo].[Client]
        ([ID_Client])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Proposal_Client'
CREATE INDEX [IX_FK_Proposal_Client]
ON [dbo].[Proposal]
    ([Id_Client]);
GO

-- Creating foreign key on [Id_Client] in table 'Receivable'
ALTER TABLE [dbo].[Receivable]
ADD CONSTRAINT [FK_Receivable_Client]
    FOREIGN KEY ([Id_Client])
    REFERENCES [dbo].[Client]
        ([ID_Client])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Receivable_Client'
CREATE INDEX [IX_FK_Receivable_Client]
ON [dbo].[Receivable]
    ([Id_Client]);
GO

-- Creating foreign key on [Id_Client] in table 'ReceivableRecurring'
ALTER TABLE [dbo].[ReceivableRecurring]
ADD CONSTRAINT [FK_ReceivableRecurring_Client]
    FOREIGN KEY ([Id_Client])
    REFERENCES [dbo].[Client]
        ([ID_Client])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReceivableRecurring_Client'
CREATE INDEX [IX_FK_ReceivableRecurring_Client]
ON [dbo].[ReceivableRecurring]
    ([Id_Client]);
GO

-- Creating foreign key on [Id_Company] in table 'ClientType'
ALTER TABLE [dbo].[ClientType]
ADD CONSTRAINT [FK_ClientType_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ClientType_Company'
CREATE INDEX [IX_FK_ClientType_Company]
ON [dbo].[ClientType]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'CostCenter'
ALTER TABLE [dbo].[CostCenter]
ADD CONSTRAINT [FK_CostCenter_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CostCenter_Company'
CREATE INDEX [IX_FK_CostCenter_Company]
ON [dbo].[CostCenter]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'Debt'
ALTER TABLE [dbo].[Debt]
ADD CONSTRAINT [FK_Debt_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Debt_Company'
CREATE INDEX [IX_FK_Debt_Company]
ON [dbo].[Debt]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'Invoice'
ALTER TABLE [dbo].[Invoice]
ADD CONSTRAINT [FK_Invoice_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Invoice_Company'
CREATE INDEX [IX_FK_Invoice_Company]
ON [dbo].[Invoice]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'Orders'
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_Orders_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Orders_Company'
CREATE INDEX [IX_FK_Orders_Company]
ON [dbo].[Orders]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [FK_Product_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Product_Company'
CREATE INDEX [IX_FK_Product_Company]
ON [dbo].[Product]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'ProductType'
ALTER TABLE [dbo].[ProductType]
ADD CONSTRAINT [FK_ProductType_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProductType_Company'
CREATE INDEX [IX_FK_ProductType_Company]
ON [dbo].[ProductType]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'Proposal'
ALTER TABLE [dbo].[Proposal]
ADD CONSTRAINT [FK_Proposal_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Proposal_Company'
CREATE INDEX [IX_FK_Proposal_Company]
ON [dbo].[Proposal]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'SecurityGroup'
ALTER TABLE [dbo].[SecurityGroup]
ADD CONSTRAINT [FK_SecurityGroup_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SecurityGroup_Company'
CREATE INDEX [IX_FK_SecurityGroup_Company]
ON [dbo].[SecurityGroup]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'Seller'
ALTER TABLE [dbo].[Seller]
ADD CONSTRAINT [FK_Seller_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Seller_Company'
CREATE INDEX [IX_FK_Seller_Company]
ON [dbo].[Seller]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'Supplier'
ALTER TABLE [dbo].[Supplier]
ADD CONSTRAINT [FK_Supplier_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Supplier_Company'
CREATE INDEX [IX_FK_Supplier_Company]
ON [dbo].[Supplier]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_Company] in table 'SupplierType'
ALTER TABLE [dbo].[SupplierType]
ADD CONSTRAINT [FK_SupplierType_Company]
    FOREIGN KEY ([Id_Company])
    REFERENCES [dbo].[Company]
        ([ID_Company])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SupplierType_Company'
CREATE INDEX [IX_FK_SupplierType_Company]
ON [dbo].[SupplierType]
    ([Id_Company]);
GO

-- Creating foreign key on [Id_CostCenter] in table 'Debt'
ALTER TABLE [dbo].[Debt]
ADD CONSTRAINT [FK_Debt_CostCenter]
    FOREIGN KEY ([Id_CostCenter])
    REFERENCES [dbo].[CostCenter]
        ([ID_CostCenter])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Debt_CostCenter'
CREATE INDEX [IX_FK_Debt_CostCenter]
ON [dbo].[Debt]
    ([Id_CostCenter]);
GO

-- Creating foreign key on [Id_CostCenter] in table 'DebtRecurring'
ALTER TABLE [dbo].[DebtRecurring]
ADD CONSTRAINT [FK_DebtRecurring_CostCenter]
    FOREIGN KEY ([Id_CostCenter])
    REFERENCES [dbo].[CostCenter]
        ([ID_CostCenter])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DebtRecurring_CostCenter'
CREATE INDEX [IX_FK_DebtRecurring_CostCenter]
ON [dbo].[DebtRecurring]
    ([Id_CostCenter]);
GO

-- Creating foreign key on [Id_CostCenter] in table 'Receivable'
ALTER TABLE [dbo].[Receivable]
ADD CONSTRAINT [FK_Receivable_CostCenter]
    FOREIGN KEY ([Id_CostCenter])
    REFERENCES [dbo].[CostCenter]
        ([ID_CostCenter])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Receivable_CostCenter'
CREATE INDEX [IX_FK_Receivable_CostCenter]
ON [dbo].[Receivable]
    ([Id_CostCenter]);
GO

-- Creating foreign key on [Id_CostCenter] in table 'ReceivableRecurring'
ALTER TABLE [dbo].[ReceivableRecurring]
ADD CONSTRAINT [FK_ReceivableRecurring_CostCenter]
    FOREIGN KEY ([Id_CostCenter])
    REFERENCES [dbo].[CostCenter]
        ([ID_CostCenter])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReceivableRecurring_CostCenter'
CREATE INDEX [IX_FK_ReceivableRecurring_CostCenter]
ON [dbo].[ReceivableRecurring]
    ([Id_CostCenter]);
GO

-- Creating foreign key on [Id_CostCenter] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [FK_Users_CostCenter]
    FOREIGN KEY ([Id_CostCenter])
    REFERENCES [dbo].[CostCenter]
        ([ID_CostCenter])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Users_CostCenter'
CREATE INDEX [IX_FK_Users_CostCenter]
ON [dbo].[Users]
    ([Id_CostCenter]);
GO

-- Creating foreign key on [Id_Country] in table 'State'
ALTER TABLE [dbo].[State]
ADD CONSTRAINT [FK_State_Country]
    FOREIGN KEY ([Id_Country])
    REFERENCES [dbo].[Country]
        ([ID_Country])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_State_Country'
CREATE INDEX [IX_FK_State_Country]
ON [dbo].[State]
    ([Id_Country]);
GO

-- Creating foreign key on [Id_Suplier] in table 'Debt'
ALTER TABLE [dbo].[Debt]
ADD CONSTRAINT [FK_Debt_Supplier]
    FOREIGN KEY ([Id_Suplier])
    REFERENCES [dbo].[Supplier]
        ([ID_Supplier])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Debt_Supplier'
CREATE INDEX [IX_FK_Debt_Supplier]
ON [dbo].[Debt]
    ([Id_Suplier]);
GO

-- Creating foreign key on [Id_Debt] in table 'DebtParcels'
ALTER TABLE [dbo].[DebtParcels]
ADD CONSTRAINT [FK_DebtParcels_Debt]
    FOREIGN KEY ([Id_Debt])
    REFERENCES [dbo].[Debt]
        ([ID_Debt])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DebtParcels_Debt'
CREATE INDEX [IX_FK_DebtParcels_Debt]
ON [dbo].[DebtParcels]
    ([Id_Debt]);
GO

-- Creating foreign key on [Id_DebtParcel] in table 'DebtPayment'
ALTER TABLE [dbo].[DebtPayment]
ADD CONSTRAINT [FK_DebtPayment_DebtParcels]
    FOREIGN KEY ([Id_DebtParcel])
    REFERENCES [dbo].[DebtParcels]
        ([ID_DebtParcels])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DebtPayment_DebtParcels'
CREATE INDEX [IX_FK_DebtPayment_DebtParcels]
ON [dbo].[DebtPayment]
    ([Id_DebtParcel]);
GO

-- Creating foreign key on [Id_RecurringType] in table 'DebtRecurring'
ALTER TABLE [dbo].[DebtRecurring]
ADD CONSTRAINT [FK_DebtRecurring_RecurringType]
    FOREIGN KEY ([Id_RecurringType])
    REFERENCES [dbo].[RecurringType]
        ([ID_RecurringType])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DebtRecurring_RecurringType'
CREATE INDEX [IX_FK_DebtRecurring_RecurringType]
ON [dbo].[DebtRecurring]
    ([Id_RecurringType]);
GO

-- Creating foreign key on [Id_Suplier] in table 'DebtRecurring'
ALTER TABLE [dbo].[DebtRecurring]
ADD CONSTRAINT [FK_DebtRecurring_Supplier]
    FOREIGN KEY ([Id_Suplier])
    REFERENCES [dbo].[Supplier]
        ([ID_Supplier])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DebtRecurring_Supplier'
CREATE INDEX [IX_FK_DebtRecurring_Supplier]
ON [dbo].[DebtRecurring]
    ([Id_Suplier]);
GO

-- Creating foreign key on [Id_Order] in table 'Invoice'
ALTER TABLE [dbo].[Invoice]
ADD CONSTRAINT [FK_Invoice_Orders]
    FOREIGN KEY ([Id_Order])
    REFERENCES [dbo].[Orders]
        ([ID_Orders])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Invoice_Orders'
CREATE INDEX [IX_FK_Invoice_Orders]
ON [dbo].[Invoice]
    ([Id_Order]);
GO

-- Creating foreign key on [Id_Seller] in table 'Invoice'
ALTER TABLE [dbo].[Invoice]
ADD CONSTRAINT [FK_Invoice_Seller]
    FOREIGN KEY ([Id_Seller])
    REFERENCES [dbo].[Seller]
        ([ID_Seller])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Invoice_Seller'
CREATE INDEX [IX_FK_Invoice_Seller]
ON [dbo].[Invoice]
    ([Id_Seller]);
GO

-- Creating foreign key on [Id_Invoice] in table 'InvoiceItens'
ALTER TABLE [dbo].[InvoiceItens]
ADD CONSTRAINT [FK_InvoiceItens_Invoice]
    FOREIGN KEY ([Id_Invoice])
    REFERENCES [dbo].[Invoice]
        ([ID_Invoice])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_InvoiceItens_Invoice'
CREATE INDEX [IX_FK_InvoiceItens_Invoice]
ON [dbo].[InvoiceItens]
    ([Id_Invoice]);
GO

-- Creating foreign key on [Id_Invoice] in table 'InvoicePayments'
ALTER TABLE [dbo].[InvoicePayments]
ADD CONSTRAINT [FK_InvoicePayments_Invoice]
    FOREIGN KEY ([Id_Invoice])
    REFERENCES [dbo].[Invoice]
        ([ID_Invoice])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_InvoicePayments_Invoice'
CREATE INDEX [IX_FK_InvoicePayments_Invoice]
ON [dbo].[InvoicePayments]
    ([Id_Invoice]);
GO

-- Creating foreign key on [Id_Product] in table 'InvoiceItens'
ALTER TABLE [dbo].[InvoiceItens]
ADD CONSTRAINT [FK_InvoiceItens_Product]
    FOREIGN KEY ([Id_Product])
    REFERENCES [dbo].[Product]
        ([ID_Product])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_InvoiceItens_Product'
CREATE INDEX [IX_FK_InvoiceItens_Product]
ON [dbo].[InvoiceItens]
    ([Id_Product]);
GO

-- Creating foreign key on [Id_UnitMeasurement] in table 'InvoiceItens'
ALTER TABLE [dbo].[InvoiceItens]
ADD CONSTRAINT [FK_InvoiceItens_UnitMeasurement]
    FOREIGN KEY ([Id_UnitMeasurement])
    REFERENCES [dbo].[UnitMeasurement]
        ([ID_UnitMeasurement])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_InvoiceItens_UnitMeasurement'
CREATE INDEX [IX_FK_InvoiceItens_UnitMeasurement]
ON [dbo].[InvoiceItens]
    ([Id_UnitMeasurement]);
GO

-- Creating foreign key on [Id_ModuleGroup] in table 'Module'
ALTER TABLE [dbo].[Module]
ADD CONSTRAINT [FK_Module_ModuleGroup]
    FOREIGN KEY ([Id_ModuleGroup])
    REFERENCES [dbo].[ModuleGroup]
        ([ID_ModuleGroup])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Module_ModuleGroup'
CREATE INDEX [IX_FK_Module_ModuleGroup]
ON [dbo].[Module]
    ([Id_ModuleGroup]);
GO

-- Creating foreign key on [Id_Module] in table 'Rights'
ALTER TABLE [dbo].[Rights]
ADD CONSTRAINT [FK_Rights_Module]
    FOREIGN KEY ([Id_Module])
    REFERENCES [dbo].[Module]
        ([ID_Module])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Rights_Module'
CREATE INDEX [IX_FK_Rights_Module]
ON [dbo].[Rights]
    ([Id_Module]);
GO

-- Creating foreign key on [Id_Order] in table 'OrderItens'
ALTER TABLE [dbo].[OrderItens]
ADD CONSTRAINT [FK_OrderItens_Orders]
    FOREIGN KEY ([Id_Order])
    REFERENCES [dbo].[Orders]
        ([ID_Orders])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderItens_Orders'
CREATE INDEX [IX_FK_OrderItens_Orders]
ON [dbo].[OrderItens]
    ([Id_Order]);
GO

-- Creating foreign key on [Id_Product] in table 'OrderItens'
ALTER TABLE [dbo].[OrderItens]
ADD CONSTRAINT [FK_OrderItens_Product]
    FOREIGN KEY ([Id_Product])
    REFERENCES [dbo].[Product]
        ([ID_Product])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderItens_Product'
CREATE INDEX [IX_FK_OrderItens_Product]
ON [dbo].[OrderItens]
    ([Id_Product]);
GO

-- Creating foreign key on [Id_UnitMeasurement] in table 'OrderItens'
ALTER TABLE [dbo].[OrderItens]
ADD CONSTRAINT [FK_OrderItens_UnitMeasurement]
    FOREIGN KEY ([Id_UnitMeasurement])
    REFERENCES [dbo].[UnitMeasurement]
        ([ID_UnitMeasurement])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderItens_UnitMeasurement'
CREATE INDEX [IX_FK_OrderItens_UnitMeasurement]
ON [dbo].[OrderItens]
    ([Id_UnitMeasurement]);
GO

-- Creating foreign key on [Id_Order] in table 'OrderPayments'
ALTER TABLE [dbo].[OrderPayments]
ADD CONSTRAINT [FK_OrderPayments_Orders]
    FOREIGN KEY ([Id_Order])
    REFERENCES [dbo].[Orders]
        ([ID_Orders])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderPayments_Orders'
CREATE INDEX [IX_FK_OrderPayments_Orders]
ON [dbo].[OrderPayments]
    ([Id_Order]);
GO

-- Creating foreign key on [Id_Proposal] in table 'Orders'
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_Orders_Proposal]
    FOREIGN KEY ([Id_Proposal])
    REFERENCES [dbo].[Proposal]
        ([ID_Proposal])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Orders_Proposal'
CREATE INDEX [IX_FK_Orders_Proposal]
ON [dbo].[Orders]
    ([Id_Proposal]);
GO

-- Creating foreign key on [Id_Seller] in table 'Orders'
ALTER TABLE [dbo].[Orders]
ADD CONSTRAINT [FK_Orders_Seller]
    FOREIGN KEY ([Id_Seller])
    REFERENCES [dbo].[Seller]
        ([ID_Seller])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Orders_Seller'
CREATE INDEX [IX_FK_Orders_Seller]
ON [dbo].[Orders]
    ([Id_Seller]);
GO

-- Creating foreign key on [Id_PageLink] in table 'ShortCut'
ALTER TABLE [dbo].[ShortCut]
ADD CONSTRAINT [FK_ShortCut_PageLink]
    FOREIGN KEY ([Id_PageLink])
    REFERENCES [dbo].[PageLink]
        ([Id_PageLink])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ShortCut_PageLink'
CREATE INDEX [IX_FK_ShortCut_PageLink]
ON [dbo].[ShortCut]
    ([Id_PageLink]);
GO

-- Creating foreign key on [Id_PageLinkSource] in table 'ShortCut'
ALTER TABLE [dbo].[ShortCut]
ADD CONSTRAINT [FK_ShortCut_PageLinkSource]
    FOREIGN KEY ([Id_PageLinkSource])
    REFERENCES [dbo].[PageLink]
        ([Id_PageLink])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ShortCut_PageLinkSource'
CREATE INDEX [IX_FK_ShortCut_PageLinkSource]
ON [dbo].[ShortCut]
    ([Id_PageLinkSource]);
GO

-- Creating foreign key on [Id_ProductType] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [FK_Product_Product_Type]
    FOREIGN KEY ([Id_ProductType])
    REFERENCES [dbo].[ProductType]
        ([ID_Product_Type])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Product_Product_Type'
CREATE INDEX [IX_FK_Product_Product_Type]
ON [dbo].[Product]
    ([Id_ProductType]);
GO

-- Creating foreign key on [Id_UnitMeasurement] in table 'Product'
ALTER TABLE [dbo].[Product]
ADD CONSTRAINT [FK_Product_UnitMeasurement]
    FOREIGN KEY ([Id_UnitMeasurement])
    REFERENCES [dbo].[UnitMeasurement]
        ([ID_UnitMeasurement])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Product_UnitMeasurement'
CREATE INDEX [IX_FK_Product_UnitMeasurement]
ON [dbo].[Product]
    ([Id_UnitMeasurement]);
GO

-- Creating foreign key on [Id_Product] in table 'ProposalItens'
ALTER TABLE [dbo].[ProposalItens]
ADD CONSTRAINT [FK_ProposalItens_Product]
    FOREIGN KEY ([Id_Product])
    REFERENCES [dbo].[Product]
        ([ID_Product])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProposalItens_Product'
CREATE INDEX [IX_FK_ProposalItens_Product]
ON [dbo].[ProposalItens]
    ([Id_Product]);
GO

-- Creating foreign key on [Id_Product] in table 'Stock'
ALTER TABLE [dbo].[Stock]
ADD CONSTRAINT [FK_Stock_Product]
    FOREIGN KEY ([Id_Product])
    REFERENCES [dbo].[Product]
        ([ID_Product])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Stock_Product'
CREATE INDEX [IX_FK_Stock_Product]
ON [dbo].[Stock]
    ([Id_Product]);
GO

-- Creating foreign key on [Id_Product] in table 'StockTransaction'
ALTER TABLE [dbo].[StockTransaction]
ADD CONSTRAINT [FK_StockTransaction_Product]
    FOREIGN KEY ([Id_Product])
    REFERENCES [dbo].[Product]
        ([ID_Product])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StockTransaction_Product'
CREATE INDEX [IX_FK_StockTransaction_Product]
ON [dbo].[StockTransaction]
    ([Id_Product]);
GO

-- Creating foreign key on [Id_Project] in table 'ServiceRequest'
ALTER TABLE [dbo].[ServiceRequest]
ADD CONSTRAINT [FK_ServiceRequest_Project]
    FOREIGN KEY ([Id_Project])
    REFERENCES [dbo].[Project]
        ([Id_Project])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ServiceRequest_Project'
CREATE INDEX [IX_FK_ServiceRequest_Project]
ON [dbo].[ServiceRequest]
    ([Id_Project]);
GO

-- Creating foreign key on [Id_Project] in table 'TroubleTicket'
ALTER TABLE [dbo].[TroubleTicket]
ADD CONSTRAINT [FK_TroubleTicket_Project]
    FOREIGN KEY ([Id_Project])
    REFERENCES [dbo].[Project]
        ([Id_Project])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TroubleTicket_Project'
CREATE INDEX [IX_FK_TroubleTicket_Project]
ON [dbo].[TroubleTicket]
    ([Id_Project]);
GO

-- Creating foreign key on [Id_Seller] in table 'Proposal'
ALTER TABLE [dbo].[Proposal]
ADD CONSTRAINT [FK_Proposal_Seller]
    FOREIGN KEY ([Id_Seller])
    REFERENCES [dbo].[Seller]
        ([ID_Seller])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Proposal_Seller'
CREATE INDEX [IX_FK_Proposal_Seller]
ON [dbo].[Proposal]
    ([Id_Seller]);
GO

-- Creating foreign key on [Id_Proposal] in table 'ProposalItens'
ALTER TABLE [dbo].[ProposalItens]
ADD CONSTRAINT [FK_ProposalItens_Proposal]
    FOREIGN KEY ([Id_Proposal])
    REFERENCES [dbo].[Proposal]
        ([ID_Proposal])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProposalItens_Proposal'
CREATE INDEX [IX_FK_ProposalItens_Proposal]
ON [dbo].[ProposalItens]
    ([Id_Proposal]);
GO

-- Creating foreign key on [Id_Proposal] in table 'ProposalPayments'
ALTER TABLE [dbo].[ProposalPayments]
ADD CONSTRAINT [FK_ProposalPayments_Proposal]
    FOREIGN KEY ([Id_Proposal])
    REFERENCES [dbo].[Proposal]
        ([ID_Proposal])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProposalPayments_Proposal'
CREATE INDEX [IX_FK_ProposalPayments_Proposal]
ON [dbo].[ProposalPayments]
    ([Id_Proposal]);
GO

-- Creating foreign key on [Id_UnitMeasurement] in table 'ProposalItens'
ALTER TABLE [dbo].[ProposalItens]
ADD CONSTRAINT [FK_ProposalItens_UnitMeasurement]
    FOREIGN KEY ([Id_UnitMeasurement])
    REFERENCES [dbo].[UnitMeasurement]
        ([ID_UnitMeasurement])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ProposalItens_UnitMeasurement'
CREATE INDEX [IX_FK_ProposalItens_UnitMeasurement]
ON [dbo].[ProposalItens]
    ([Id_UnitMeasurement]);
GO

-- Creating foreign key on [Id_Receivable] in table 'ReceivableParcels'
ALTER TABLE [dbo].[ReceivableParcels]
ADD CONSTRAINT [FK_ReceivableParcels_Receivable]
    FOREIGN KEY ([Id_Receivable])
    REFERENCES [dbo].[Receivable]
        ([ID_Receivable])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReceivableParcels_Receivable'
CREATE INDEX [IX_FK_ReceivableParcels_Receivable]
ON [dbo].[ReceivableParcels]
    ([Id_Receivable]);
GO

-- Creating foreign key on [Id_ReceivableParcel] in table 'ReceivablePayment'
ALTER TABLE [dbo].[ReceivablePayment]
ADD CONSTRAINT [FK_ReceivablePayment_ReceivableParcels]
    FOREIGN KEY ([Id_ReceivableParcel])
    REFERENCES [dbo].[ReceivableParcels]
        ([ID_ReceivableParcels])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReceivablePayment_ReceivableParcels'
CREATE INDEX [IX_FK_ReceivablePayment_ReceivableParcels]
ON [dbo].[ReceivablePayment]
    ([Id_ReceivableParcel]);
GO

-- Creating foreign key on [Id_RecurringType] in table 'ReceivableRecurring'
ALTER TABLE [dbo].[ReceivableRecurring]
ADD CONSTRAINT [FK_ReceivableRecurring_RecurringType]
    FOREIGN KEY ([Id_RecurringType])
    REFERENCES [dbo].[RecurringType]
        ([ID_RecurringType])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ReceivableRecurring_RecurringType'
CREATE INDEX [IX_FK_ReceivableRecurring_RecurringType]
ON [dbo].[ReceivableRecurring]
    ([Id_RecurringType]);
GO

-- Creating foreign key on [Id_SecurityGroup] in table 'Rights'
ALTER TABLE [dbo].[Rights]
ADD CONSTRAINT [FK_Rights_SecurityGroup]
    FOREIGN KEY ([Id_SecurityGroup])
    REFERENCES [dbo].[SecurityGroup]
        ([ID_SecurityGroup])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Rights_SecurityGroup'
CREATE INDEX [IX_FK_Rights_SecurityGroup]
ON [dbo].[Rights]
    ([Id_SecurityGroup]);
GO

-- Creating foreign key on [Id_SecurityGroup] in table 'SecurityGroup_Users'
ALTER TABLE [dbo].[SecurityGroup_Users]
ADD CONSTRAINT [FK_SecurityGroup_Users_SecurityGroup]
    FOREIGN KEY ([Id_SecurityGroup])
    REFERENCES [dbo].[SecurityGroup]
        ([ID_SecurityGroup])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SecurityGroup_Users_SecurityGroup'
CREATE INDEX [IX_FK_SecurityGroup_Users_SecurityGroup]
ON [dbo].[SecurityGroup_Users]
    ([Id_SecurityGroup]);
GO

-- Creating foreign key on [Id_Users] in table 'SecurityGroup_Users'
ALTER TABLE [dbo].[SecurityGroup_Users]
ADD CONSTRAINT [FK_SecurityGroup_Users_Users]
    FOREIGN KEY ([Id_Users])
    REFERENCES [dbo].[Users]
        ([ID_Users])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SecurityGroup_Users_Users'
CREATE INDEX [IX_FK_SecurityGroup_Users_Users]
ON [dbo].[SecurityGroup_Users]
    ([Id_Users]);
GO

-- Creating foreign key on [Id_ServiceRequest] in table 'ServiceRequest_Historic'
ALTER TABLE [dbo].[ServiceRequest_Historic]
ADD CONSTRAINT [FK_ServiceRequest_Historic_ServiceRequest]
    FOREIGN KEY ([Id_ServiceRequest])
    REFERENCES [dbo].[ServiceRequest]
        ([Id_ServiceRequest])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ServiceRequest_Historic_ServiceRequest'
CREATE INDEX [IX_FK_ServiceRequest_Historic_ServiceRequest]
ON [dbo].[ServiceRequest_Historic]
    ([Id_ServiceRequest]);
GO

-- Creating foreign key on [Id_User] in table 'ServiceRequest'
ALTER TABLE [dbo].[ServiceRequest]
ADD CONSTRAINT [FK_ServiceRequest_User]
    FOREIGN KEY ([Id_User])
    REFERENCES [dbo].[Users]
        ([ID_Users])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ServiceRequest_User'
CREATE INDEX [IX_FK_ServiceRequest_User]
ON [dbo].[ServiceRequest]
    ([Id_User]);
GO

-- Creating foreign key on [Id_ServiceRequest] in table 'TimeSheet'
ALTER TABLE [dbo].[TimeSheet]
ADD CONSTRAINT [FK_TimeSheet_ServiceRequest]
    FOREIGN KEY ([Id_ServiceRequest])
    REFERENCES [dbo].[ServiceRequest]
        ([Id_ServiceRequest])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TimeSheet_ServiceRequest'
CREATE INDEX [IX_FK_TimeSheet_ServiceRequest]
ON [dbo].[TimeSheet]
    ([Id_ServiceRequest]);
GO

-- Creating foreign key on [Id_User] in table 'ServiceRequest_Historic'
ALTER TABLE [dbo].[ServiceRequest_Historic]
ADD CONSTRAINT [FK_ServiceRequest_Historic_Users]
    FOREIGN KEY ([Id_User])
    REFERENCES [dbo].[Users]
        ([ID_Users])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ServiceRequest_Historic_Users'
CREATE INDEX [IX_FK_ServiceRequest_Historic_Users]
ON [dbo].[ServiceRequest_Historic]
    ([Id_User]);
GO

-- Creating foreign key on [Id_TransactionType] in table 'StockTransaction'
ALTER TABLE [dbo].[StockTransaction]
ADD CONSTRAINT [FK_StockTransaction_TransactionType]
    FOREIGN KEY ([Id_TransactionType])
    REFERENCES [dbo].[TransactionType]
        ([ID_TransactionType])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StockTransaction_TransactionType'
CREATE INDEX [IX_FK_StockTransaction_TransactionType]
ON [dbo].[StockTransaction]
    ([Id_TransactionType]);
GO

-- Creating foreign key on [Id_SupplierType] in table 'Supplier'
ALTER TABLE [dbo].[Supplier]
ADD CONSTRAINT [FK_Supplier_SupplierType]
    FOREIGN KEY ([Id_SupplierType])
    REFERENCES [dbo].[SupplierType]
        ([ID_SupplierType])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Supplier_SupplierType'
CREATE INDEX [IX_FK_Supplier_SupplierType]
ON [dbo].[Supplier]
    ([Id_SupplierType]);
GO

-- Creating foreign key on [Id_Supplier] in table 'SupplierAddress'
ALTER TABLE [dbo].[SupplierAddress]
ADD CONSTRAINT [FK_SupplierAdress_Supplier]
    FOREIGN KEY ([Id_Supplier])
    REFERENCES [dbo].[Supplier]
        ([ID_Supplier])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SupplierAdress_Supplier'
CREATE INDEX [IX_FK_SupplierAdress_Supplier]
ON [dbo].[SupplierAddress]
    ([Id_Supplier]);
GO

-- Creating foreign key on [Id_Supplier] in table 'SupplierContact'
ALTER TABLE [dbo].[SupplierContact]
ADD CONSTRAINT [FK_SupplierContact_Supplier]
    FOREIGN KEY ([Id_Supplier])
    REFERENCES [dbo].[Supplier]
        ([ID_Supplier])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SupplierContact_Supplier'
CREATE INDEX [IX_FK_SupplierContact_Supplier]
ON [dbo].[SupplierContact]
    ([Id_Supplier]);
GO

-- Creating foreign key on [Id_TroubleTicket] in table 'TimeSheet'
ALTER TABLE [dbo].[TimeSheet]
ADD CONSTRAINT [FK_TimeSheet_TroubleTicket]
    FOREIGN KEY ([Id_TroubleTicket])
    REFERENCES [dbo].[TroubleTicket]
        ([Id_TroubleTicket])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TimeSheet_TroubleTicket'
CREATE INDEX [IX_FK_TimeSheet_TroubleTicket]
ON [dbo].[TimeSheet]
    ([Id_TroubleTicket]);
GO

-- Creating foreign key on [Id_User] in table 'TimeSheet'
ALTER TABLE [dbo].[TimeSheet]
ADD CONSTRAINT [FK_TimeSheet_User]
    FOREIGN KEY ([Id_User])
    REFERENCES [dbo].[Users]
        ([ID_Users])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TimeSheet_User'
CREATE INDEX [IX_FK_TimeSheet_User]
ON [dbo].[TimeSheet]
    ([Id_User]);
GO

-- Creating foreign key on [Id_TroubleTicket] in table 'TroubleTicket_Historic'
ALTER TABLE [dbo].[TroubleTicket_Historic]
ADD CONSTRAINT [FK_TroubleTicket_Historic_TroubleTicket]
    FOREIGN KEY ([Id_TroubleTicket])
    REFERENCES [dbo].[TroubleTicket]
        ([Id_TroubleTicket])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TroubleTicket_Historic_TroubleTicket'
CREATE INDEX [IX_FK_TroubleTicket_Historic_TroubleTicket]
ON [dbo].[TroubleTicket_Historic]
    ([Id_TroubleTicket]);
GO

-- Creating foreign key on [Id_User] in table 'TroubleTicket'
ALTER TABLE [dbo].[TroubleTicket]
ADD CONSTRAINT [FK_TroubleTicket_User]
    FOREIGN KEY ([Id_User])
    REFERENCES [dbo].[Users]
        ([ID_Users])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TroubleTicket_User'
CREATE INDEX [IX_FK_TroubleTicket_User]
ON [dbo].[TroubleTicket]
    ([Id_User]);
GO

-- Creating foreign key on [Id_User] in table 'TroubleTicket_Historic'
ALTER TABLE [dbo].[TroubleTicket_Historic]
ADD CONSTRAINT [FK_TroubleTicket_Historic_Users]
    FOREIGN KEY ([Id_User])
    REFERENCES [dbo].[Users]
        ([ID_Users])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_TroubleTicket_Historic_Users'
CREATE INDEX [IX_FK_TroubleTicket_Historic_Users]
ON [dbo].[TroubleTicket_Historic]
    ([Id_User]);
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------